0

Input/File

A:1111
B:21222
C:33rf33
D:444dct4
E:5tdffe
F:4444we
G:j5555
H:46666
I:efe989ef
J:efee

Basically need to select the line that contains 2122 (i.e line B/2) & line which starts with 444dct4 (i.e Line D) till efe989ef (i.e line I/9) To summarize

Select Line B (contains 2122)
Select Line D (444dct4) till Line I

Desired Output

B:21222
D:444dct4
E:5tdffe
F:4444we
G:j5555
H:46666
I:efe989ef
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Prasad V
  • 3
  • 3
  • 1
    Welcome to SO, could you please do add your efforts in your question which is highly encouraged on SO. Also please do explain Logic of getting expected output more clearly in your question. – RavinderSingh13 Dec 22 '20 at 08:30
  • I think this question is a duplicate to https://stackoverflow.com/questions/12182910/using-awk-to-pull-specific-lines-from-a-file – Karim Tawfik Dec 22 '20 at 08:31
  • Also, do you want _the line that contains 2122 **OR** (i.e line B **OR** 2)_ – James Brown Dec 22 '20 at 08:33
  • Thanks James. The line that contains 2122 – Prasad V Dec 22 '20 at 09:00
  • @KarimTawfik I suppose the question is relating to 2 separate files if i am understanding correctly. I need have a single file & need the desired output – Prasad V Dec 22 '20 at 09:02

3 Answers3

3

Could you please try following, written and tested with shown samples in GNU awk. This one also takes care in case line's 2nd column 21222 in between range of 444dct4 to efe989ef then it will NOT re-print it.

awk -F':' '
$2=="21222" && !found{
  print
  next
}
$2=="444dct4"{
  found=1
}
found
$2=="efe989ef"{
  found=""
}
'  Input_file

Explanation: Adding detailed explanation for above.

awk -F':' '              ##Starting awk program from here and setting field separator as colon here.
$2=="21222" && !found{   ##Checking if 2nd field is 21222 and found is NOT set then try following.
  print                  ##Printing the current line here.
  next                   ##next will skip all further statements from here.
}
$2=="444dct4"{           ##Checking condition if 2nd field is 444dct4 then do following.
  found=1                ##Setting found to 1 here.
}
found                    ##Checking condition if found is SET then print that line.
$2=="efe989ef"{          ##Checking condition if 2nd field is efe989ef then do following.
  found=""               ##Nullifying found here.
}
'  Input_file            ##Mentioning Input_file name here.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
2
$ awk -F: '
/2122/ {                      # line that contains 2122
    print                  
    next                      # to avoid duplicate printing if 2122 also in D-I
}
$2~/^444dct4/,$2~/efe989ef/   # starts with 444dct4 till efe989ef
' file

Output:

B:21222
D:444dct4
E:5tdffe
F:4444we
G:j5555
H:46666
I:efe989ef

Edit:

One-liner:

$ awk -F: '/2122/{print; next} $2~/^444dct4/,$2~/efe989ef/' file.txt 
James Brown
  • 36,089
  • 7
  • 43
  • 59
1
awk -v str1="2122" -v str2="444dct4" -v str3="efe989ef" 'BEGIN { flag=0 } $0 ~ str1 { print } $0 ~ str2 { flag=1 } $0 ~ str3 { flag=0;print;next } flag' file

For flexibility, set the line to find as str1, the from as str2 and the to as str3. Set a print flag (flag) to begin with. When 2122 is in the second field print. Then when the second field begins with 44dct4 set the print flag to one. When the second field starts with efe989ef, set the print flag to 0, print the line and skip to the next record. The variable flag will then determine what does and doesn't get printed.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18