The awk below executes and produces the current output. it skips the header in row 1 and prints $4,$5,$6 and then adds the header row back.
The problem is that it keeps the tailing tab and prints it in front of $1. I could add a pipe to remove the tab, but is there a better way to do it with on command? Thank you .
File tab delimeted
Current output
Desired output
awk
so if I understand this the first three fields are skipped and the 4-6 are printed separated by a tab and since the first three fields are skipped so is the trailing tab in the 3rd field?
This skips row 1 and prints after field 3 then loops through 4-6 printing them. I think $(i-3)=$i tells awk to loop through each line and the NF-=3 prints fields 4-6 and then the header is added?
I guess my question is in the first awk how does the header row get added?
Both commands work great, just trying to understand. Thank you .
so if I understand this the first three fields are skipped and the 4-6 are printed separated by a tab and since the first three fields are skipped so is the trailing tab in the 3rd field?
The awk print statement says exactly what is to be printed. Since fields 1, 2, and 3 (and the field separators following them) are not mentioned, they are not printed. The FNR==1 (with no specified action) performs the default action (print $0) when that condition evaluates to true. That condition will evaluate to true when the first line of an input file is read (i.e., when the header line of an input file is read). This was taken directly from your code
Quote:
This skips row 1 and prints after field 3 then loops through 4-6 printing them. I think $(i-3)=$i tells awk to loop through each line and the NF-=3 prints fields 4-6 and then the header is added?
I guess my question is in the first awk how does the header row get added?
The condition and action pair:
says that for each input line that has a record number (in all input files) greater than 1 AND has more than three fields, do the following. First copy each field with a field number greater than or equal to 4 to the field with field number 3 less than the field being copied. Then reduce the number of fields in the current record by 3. And, then print the updated record.
Note that since your sample input only has 3 fields on the first line, the NR>1 is a no-op and can be omitted.
The header is printed by both of our scripts exactly the same way it was printed by your script. As explained above, the FNR==1 in all of our scripts causes the first line of each input file read to be printed unchanged.
Quote:
Both commands work great, just trying to understand. Thank you .
I hope this helps you understand.
This User Gave Thanks to Don Cragun For This Post:
Hi rdrtx1,
I guess I don't understand why you posted this code. With the sample input provided, the above code copies the input file to the output (since no input line has more than 6 fields).
The command:
would print just the requested lines but does not discard the first three fields on lines with 6 fields.
Note well: This post has been updated. I got the logic backwards when I first read the script. The first paragraph now correctly specifies how the quoted code behaves. I apologize for any confusion this may have caused.
Last edited by Don Cragun; 07-30-2019 at 01:10 AM..
Reason: Fix description of quoted code behavior.
This User Gave Thanks to Don Cragun For This Post:
Hi
I have a file profile.txt with the below input:
{"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"y
es"},
{"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto... (6 Replies)
In the below awk written by @RavinderSingh13 I have added a few lines and am trying to have the output be tab-delimited. The input is space-delimeted and the portion in bold seems to add a tab to the Not found but not the found. Thank you :).
file1
One 1
Two 2
Three 3
file2
One 1... (2 Replies)
In the below awk the output is space delimited, but it should be tab delimited. Did I not add the correct -F and OFS? Thank you :).
The input file are rather large so I did not include them, but they are tab-delimeted files as well.
awk
awk -F'\t' -v OFS='\t' 'FNR==1 { next }
> ... (2 Replies)
I am just trying to output the below awk separated by tabs. Thank you :).
awk (added OFS as an attempt to itroduce tabs)
awk '{split($5,a,"-"); OFS='\t' print $1,$2,$3,a}' file.bed > test.bed
The awk runs and produces all the data in 1 field instead of 4 fields.
current output
... (2 Replies)
Hi,
I need to search for a word using Awk and print out the line the word is in and every line after the search phrase until I hit this #------------. Then I need to send it to a csv file.
So basically the input file format is like this:... (1 Reply)
I have the following script that renames filenames like:
blah_bleh_91_2011-09-26_00.05.43AM.xls
and transforms it in:
91_20110926_000543_3_blih.xls
for a in *.xls;
do
b="$(echo "${a}" | cut -d '_' -f4)"
dia=`echo ${b} | cut -c9-10`
mes=`echo ${b} | cut -c6-7`
anio=`echo ${b} | cut -c1-4`... (4 Replies)
Dear friends,
I want to print variables' values in certain format where space between two values of variables is "a tab"
I tried where I provided "tab" between two varibales.
But when it print values on screen its giving me output without spaces in two values.
Request you to help me in... (7 Replies)
Dear All,
Good Day. I would like to hear your suggestions for the following problem:
I have a file with 5 columns with some numbers in 16 lines as shown below.
Input file:
Col 1 Col 2 Col 3 Col 4 Col 5
12 220 2 121 20
234 30 22 9... (3 Replies)
friends, i am a newbie in scripting. could someone help me in selecting only the last column of below ps command output ?
mqm 14 16466 0 Sep 15 ? 0:01 /opt/mqm/bin/runmqlsr -r -m QMGR.INBOUNDSSL -t TCP -p 1415 -i 5.1.26.5
mqm 12 16700 0 Sep 15 ? 0:00... (4 Replies)
At work, when I issue:
lpr -P$PRINTER -h filename
the file always prints a line then does a vertical tab off that first line instead of starting the new line at left.
It does this with every file I've tried, and I can't find any options that are set that would make things print this way.
I... (2 Replies)