Score:0

grep date from 2nd line of a text file

ng flag

I have text file named (document_determinationv1.txt). This text file contains two lines which is mentioned below. I want to get the date(20210805) from the second line and want to store in a variable using shell script commands. I am new to Linux. Thanks in advance.

[DOCUMENT_DETERMINATION]
#REVISION:v1;DATE:20210805
ng flag
[DOCUMENT_DETERMINATION]
Score:2
cn flag

As always, there's more than one way to do it, depending on the exact requirements.

As one of the simplest ways, you could do:

AVARIABLE=$(sed -ne 's/.*DATE://p' document_determinationv1.txt)

This doesn't actually care which line the date is on. It will work as long as your file contains exactly one line containing the keyword DATE followed by a colon and the date you want to extract. It will also not check whether DATE appears as a word of its own, whether the part after DATE: is actually a date, or whether it is followed by any additional junk. If your file contains more than one line containing DATE followed by a colon then it will put all of the parts after each DATE: into the variable, separated by newlines, which may or may not wreak havoc with your further processing. So if your file contains, for example:

[DOCUMENT_DETERMINATION]
#REVISION:v1;DATE:20210805;WEIGHT:123kg
[REAL_DETERMINATION]
#PRIME:13;CANDIDATE:BART SIMPSON

then the command will happily put

20210805;WEIGHT:123kg
BART SIMPSON

into the variable, including the newline between the letters g and B.

But as long as you can guarantee the file has exactly the format you quoted in your question it will work fine.

hr flag
Of course you can easily make it care which line it is on - by changing `s` to `2s` for example
ng flag
It worked with this command what u provided.Thanks a lot.
Score:0
gb flag

You could do the following:

cat file.txt | sed "2q;d" | awk -F: '{print $3}'
  • cat prints out the file content into a pipe
  • sed takes this pipe and outputs the second line
  • which awk takes, splits by colons and finally prints the third argument

You can try each command separately. And if you want to store the whole stuff into a variable, use the $(command) syntax:

VAR=$(cat file.txt | sed "2q;d" | awk -F: '{print $3}')

And print the result:

echo $VAR
hr flag
You don't need `cat`, and you don't need sed if you use awk's `NR` to choose the line ex. `awk -F'[;:]' 'NR==2 {print $4}' document_determinationv1.txt`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.