0

below is the file from which the data should be copied from a specific line and specific location

 CP        I  ({010010010010010010010010010010}000 000)  234764-CTS_fsdfs_a_inv_33ghf/ZN
 ---       I  ({111100011111100000000000011100}111 111)  2518255-
 Q         O  ({001111100011111100000000000011}111 **1**11)  2520618-

AP I ({010010010010010010010010010010}010 010) 23499764-fdsf_ccl_a_inv_3330gg/XX --- I ({111111111111111111111100000011}000 000) 2518255- Q O ({011111110011111111111111100000}000 000) 2909918-

QP I ({110010010010010010010010010010}010 010) 234764-ZZZ_ccl_a_inv_33305/ZZ --- I ({111111111111111111111100000011}000 000) 2518255- Q O ({011111111111111111111111100000}000 111) 2599918-

NOTE: the ** are not present in the original file it is there just to help to recognise the digit which to get print into the new file

i have to print the bold letter of the line Q only. and save the content into a new file line by line. And please suggest some code without $3 type of command because while using awk command with $3 it gives error like :

can't read "3": no such variable

Your help is really appreciated

vikas
  • 1

2 Answers2

0

You can try something like:

awk '$1=="Q"{split($4,a,""); print a[1]}' input_file >new_file

The idea is to use split with empty delimiter to each character in own element.

Also as suggested you can use

awk '$1=="Q"{print substr($4,1,1)}' input_file >new_file
Romeo Ninov
  • 17,484
0

Assuming that no line has any initial whitespace (the lines in the question seem to have a variable number of spaces at the start, but I'm assuming this is due to uncareful copy-and-paste) and that the format is otherwise fixed with regards to the width of fields etc.:

$ cut -c 1,2,51 file | sed -n 's/^Q //p'
1
0
1

This extracts the two first and the 51:st characters from each line. The two first characters are used by sed to determine what lines to output (after removing the first two characters). The 51:st character is the digit that you are looking for.

Alternatively (and possibly simpler), using grep and cut:

$ grep '^Q ' file | cut -c 51
1
0
1

Here, the interesting lines are extracted with grep, and then the 51:st character from each is pulled out with cut. Again, this only works if the file's format has fixed width fields and no initial whitespace.

Kusalananda
  • 333,661