I'd like use awk to parse a file, and add a line, if and only if this line does not already exist. My file: cccc dddd aaaa mmm  Example 1: I'm looking for &quot;aaaa&quot; &quot;aaaa&quot; exists so, nothing happens, the output file will be the same. Example 2: I'm looking for &quot;bbbb&quot; &quot;bbbb&quot; doesn't exist. My output file should be : cccc dddd aaaa mmm bbbb  How can I obtain this result?
123468
Using awk, how we can add text at the bottom, only if a certain pattern was not found in the file?    This is not working:    awk '{if ($0 !~ /pattern/) {print}; END {print "someText"}}' file   The file is small, less than 1MB.
37242
Is it possible to do a multiline pattern match using sed, awk or grep? Take for example, I would like to get all the lines between { and }  So it should be able to match    1. {}  2. {.....}  3. {.....      .....}  Initially the question used &lt;p&gt; as an example. Edited the question to use { and}.
57610
I have more than 1000 lines in a file. The file starts as follows (line numbers added):  Station Name Station Code A N DEV NAGAR ACND ABHAIPUR AHA ABOHAR ABS ABU ROAD ABR   I need to convert this to a file, with comma separated entries by joining every two lines. The final data should look like  Station Name,Station Code A N DEV NAGAR,ACND ABHAIPUR,AHA ABOHAR,ABS ABU ROAD,ABR ...   What I was trying was - trying to write a shell script and then echo them with comma in between. But I guess a simpler effective one-liner would do the job here may be in sed/awk.  Any ideas?
26898
I have this script to divide a file over a 100000 or even more in 50000 lines.  desc()                                                     {                                                       echo $1 $2|awk '{d=$1;}                                 BEGIN {a=1;b=0}                                            { f=d"0"a;                                                while ((getline x&lt;d)&gt;0)                                   { print x&gt;&gt;f".txt"                                        if (b++&gt;=50000)                                            {                                                         close(f);b=0;a++;f=(a&lt;10)?d"0"a:f=d""a                }                                                  }                                                   }                                                   END {print "Terminado ... ! "}'                         }                                                      if [ -f $1 ]; then                                              desc $1 $2                                      fi                                                        When I execute it I got this error message:  sh-3.2$ parte.sh pa.txt                                  : command not found                                      '/parte.sh: line 2: syntax error near unexpected token ` '/parte.sh: line 2: `desc()                              sh-3.2$                                                    Can anyone help to solve it?
27386
I have large 3-column files (~10,000 lines) and I would like to remove lines when the contents of the third column of that line appear in the third column of another line. The files' sizes make sort a bit cumbersome, and I can't use something like the below code because the entire lines aren't identical; just the contents of column 3.  awk '!seen[$0]++' filename
52516
I am trying to sum certain numbers in a column using awk. I would like to sum just column 3 of the "smiths" to get a total of 212. I can sum the whole column using awk but not just the "smiths". I have:  awk 'BEGIN {FS = "|"} ; {sum+=$3} END {print sum}' filename.txt   Also I am using putty. Thank you for any help.  smiths|Login|2 olivert|Login|10 denniss|Payroll|100 smiths|Time|200 smiths|Logout|10
114129
I have a big csv file (Test.csv), which looks like this: 1,2,3,A,5 1,2,3,B,5 1,2,3,E,5 1,2,3,D,5 1,2,3,Z,5 1,2,3,B,5  I want to print the lines in which the 4th column has the same content in different files. Actually, I need to join these lines that have the same content in a new csv or txt file, named as the 4th column content. For example: Output: File A 1,2,3,A,5 1,2,3,A,5 1,2,3,A,5  File B 1,2,3,B,5 1,2,3,B,5  Since the input file is large, I have no idea how many different patterns there are in this 4th column. Column 4 contains only words and the other columns contain words and/or numbers. As I have no experience, I researched similar questions and even tried the following code: awk 'NR==FNR{a[$4]=NR; next} $NF in a {print &gt; &quot;outfile&quot; a[$NF]}' Test.csv  but nothing worked. Can anyone help me, please? Thanks in advance.
61269
Is there a way to modify a file without writing the contents to another file, without sed and awk?  For example:  $ cat test.txt aaa aaa bbb ccc ddd   Replacing using sed with -i option, sed -i 's/aaa/NNN/g' test.txt will produce the following:  NNN NNN bbb ccc ddd   How to do that without awk and sed?
31972
Suppose I have a file that contains multiple occurrences of both StringA and StringB. I want to replace all occurrences of StringA with StringB, and (simultaneously) all occurrences of StringB with StringA.  Right now, I'm doing something like  cat file.txt | sed 's/StringB/StringC/g' | sed 's/StringA/StringB/g' | sed 's/StringC/StringA/g'   The problem with this approach is that it assumes StringC doesn't occur in the file. While this isn't an issue in practice, this solution still feels dirty -- that is, it feels like an opportunity to learn more unix magic. :)
44297
I am using awk below to print 8th column and remove duplicates in that very column.  awk -F "," '{print $8}' filecsv | awk '!NF || !seen[$0]++'   How to do it with just one awk instead running awk twice in the above pipline
28898
I have a text file:  a   aa  aaa     b   bb  bbb     c   cc  ccc d   dd  ddd     e   ee  eee     f   ff  fff g   gg  ggg     h   hh  hhh     i   ii  iii j   jj  jjj   How can I process it and get a 2 column file like this:  a   aa aaa b bb  bbb c   cc ccc d dd  ddd e   ee eee f ff  fff g   gg ggg h hh  hhh i   ii iii j jj  jjj   Or a three column file like this:  a   aa  aaa b   bb  bbb c   cc  ccc d   dd  ddd e   ee  eee f   ff  fff g   gg  ggg h   hh  hhh i   ii  iii j   jj  jj   I prefer to get awk solution but other solutions are welcomed too.
121723
I'm having an issue with the following piece of (G)AWK script:  do {     ... } while (system("sleep 10"))   My intention is to break the loop when the user presses ^C during the sleep, but it's not working.  I believe the problem is that Bash exits with 0 when interrupted with ^C, at least when it's executed by AWK's system():  $ awk 'BEGIN { print "\n" system("sleep 2") }' (let the sleep complete) 0      $ awk 'BEGIN { print "\n" system("sleep 2") }' ^C 0   Why is it so?   Is this a bug in Bash or in (G)AWK?  Is there a simple solution that doesn't involve complicated Bash-specific syntax like trap?  The best I could come up with is this:  do {     ... } while (42 == system("sleep 10 &amp;&amp; exit 42"))   which still looks like a kludge to me.
2306
I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:  Host localhost     ForwardAgent yes   So "line then new line 'tab' then text" I think its a sensitive format.  I know you can do this:  cat temp.txt &gt;&gt; data.txt   But it seems weird since its two lines. Is there a way to append that in this format:  echo "hello" &gt;&gt; greetings.txt
51106
Let M=3 and N=4. I want to replace line M in file1 with line N in file2. I can replace with a string using sed:  sed -i '3s/.*/stringToReplace/' file1   And I can use awk to get line N from file2  awk 'NR==4' "file2"   How can I combine these two? If I try  sed -i '3s/.*/{awk 'NR==4' "file2"}/' file1   then I replace line M with literally {awk 'NR==4' "file2"}.
7069
Using a common command line tool like sed or awk, is it possible to join all lines that end with a given character, like a backslash?  For example, given the file:  foo bar \ bash \ baz dude \ happy   I would like to get this output:  foo bar bash baz dude happy
2320
We will generate a csv file with below values  yp1234,577,1,3 yp5678,577,3,5 yp9012,132,8,9   I need to extract data and create files based on second column. If it's 577 then the whole line has to be extracted and placed in a separate file. I mean I need a file having lines with second column as 577 alone and another file with second column as 132 alone  I tried using IF but didn't work
117864
I have the file:  key value  blah blah blah blah blahblah man1 boy1 blah blah man1 boy2 man1 boy1   I  do this to remove duplicate lines:  awk '/man1/ { print $1,$2} ' file | awk '!x[$0]++'   and the command take the first line and ignore other lines  man1 boy1  man1 boy2   but I want to ignore all lines except the last line:  man1 boy2  man1 boy1   as ramesh said I want something like:  cat filename blah blah blah blah blahblah man1 boy1 blah blah man1 boy2 man1 boy1 man1 boy2 man1 boy3 man1 boy4 man1 boy2   the desired output   man1 boy1 man1 boy3 man1 boy4 man1 boy2