The awk below executes and produces the current output, which is correct, except I can not seem to include the header lines # and ## in the output as well. I tried adding !/^#/ thinking that it would skip the lines with # and output them but the entire file prints as is. Thank you .
Works great, thank you. I am currently learning python (or trying) and was going to use the awk as practice.... that is try rewriting it in python. Could I post back comments on each line to see if my thinking is correct? Thank you .
awk
/^#/ { print;next} # retains header as well
Last edited by cmccabe; 02-27-2019 at 02:18 PM..
Reason: commented awk
Could I post back comments on each line to see if my thinking is correct?
Of course you can do that - in fact you are explicitly encouraged to do so. This forum is all about self-empowerment and learning to help yourself. But you probably knew that already, didn't you?
A major difference between awk and sed is that the latter outputs every line, changed or not, by default. i.e.
will not only output all lines containing "old" with "old" changed to "NEW" but also all other lines, simply without any change at all. awk works different and will only output what it is explicitly told to output - through the print command or whatever means. Therefore, if there is no rule to print lines starting with a "#" then these lines will not be printed.
Quote:
Originally Posted by cmccabe
define FS as pattern of tab and semi-colon
Not quite: FS is defined as either a tab or a semicolon. [....] is a so-called "character-class" and often used in regexps. It always means "one of the enclosed characters". i.e. d[ae]n would match either "dan" or "den" but neither "dean" nor "daen". There is the possibility of grouping characters instead of enumerating them, i.e [a-z] is "any (non-capitalised) character a-z" and [a-zA-Z] is "any character a-z, capitalised or not".
You can also negate these classes by using "^" as first character: [^0-9] is "anything but a digit".
I hope this helps.
bakunin
Last edited by bakunin; 02-27-2019 at 02:28 PM..
These 2 Users Gave Thanks to bakunin For This Post:
I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty.
I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
In the awk below I am trying to print only the header lines starting with # or ## and the lines that $7 is PASS and AF= is less than 5%. The awk does execute but returns an empty file and I am not sure what I am doing wrong. Thank you.
file
... (0 Replies)
I am trying to output all lines in a file where $7 is less than 30. The below code does create a result file, but with all lines in the original file. The original file is tab deliminated is that the problem? Thank you :).
awk 'BEGIN{FS=OFS=","} $7 < 30 {print}' file.txt > result.txt... (3 Replies)
Hello There...
I have a sample input file ..
number:department:amount
125:Market:125.23
126:Hardware store:434.95
127:Video store:7.45
128:Book store:14.32
129:Gasolline:16.10
I will be doing some manipulations on all the records except the header, but the header should always be... (2 Replies)
I have a file with very specific column spacing formatting,
I wish to do the following:
awk '{print $1, $2, $3, $4, $5, $6, $19-$7, $20-$8, $21-$9, $10, $11, $12}' merge.pdb > vector.pdb
but the format gets ruined.
I have tried with print -f but to no avail.... (7 Replies)
Hi,
Is there a way to write to a txt file each day but retain the header on the file? I'm cat'ing 5 files into one .txt file each day but I want the new data to be written after the first 2 lines which are:
Progname Size Date Owner
----------------------------
Basically I want my new... (4 Replies)
Hi all,
I have space delimated file which look like this
1 2 3 4
5 6 7 8
9 1 0 11
I am using simple awk command to read the second column
awk '{print $2}' input_file
but i got the output like this which also read 10 from the third column
2
6... (8 Replies)
Hello Friends,
I have got, this output below and i want to extract the name of symlink which is highlighted in red and the path above it highlighted in blue. At the end i want to append path and symlink.
/var/tmp/asirohi/jdk/jre
/var/tmp/asirohi/jdk/jre/.systemPrefs... (3 Replies)
Hi,
I am using Bash shell to create some data and these data would be piped out to a file, let say output.txt.
This output.txt I would like to add some extra header information such as comments, descriptions and general information on the text.
I would like to know how could I maintain... (0 Replies)