I have the exact same question as this one . Only difference is I have 3 columns only and I am trying to move the 1st column to the end.  The original file looks like this:  col1,col2,col3 2,2015-01-04,23 196,2015-01-20,36   I didn't make the mistake that other questioner did (i.e. not put comma after F or after OFS=). So, my code is  awk -F, '{print $2,$3,$1}' OFS=, old.csv &gt; new.csv   But I am getting the 3rd column (which used to be the first) in a new line:  col1,col2,col3 2015-01-04,23 ,2 2015-01-20,36 ,196   Why is awk sending the 3rd column data to a new line? I am using awk on Linux Bash Shell (Ubuntu) on Windows, downloaded from .
5146
I've created a bash script but when I try to execute it, I get   #!/bin/bash no such file or directory   I need to run the command: bash script.sh for it to work.  How can I fix this?
77595
DATA SET file name "demo.txt"  ID|SAL|COL|PER|TAG|GER 1"|"1.11"|"2.22"|"1.1"|"2.2"|D" 2"|"1.234"|"3.234"|"2.2222"|"2.34"|"B" 3"|"1.234"|"35.23"|"3.2"|"2.34"|"A"   I have created a function which will automatically calculate decimal column sum below is the function I am calling that function in main.sh script   demo() { FILE_NAME="$1" COLUMN_NAME="$2"  alpha=( $(awk -F"|" 'NR==1{for(i=1;i&lt;=NF;i++){if ($i ~ /'$COLUMN_NAME'/){print i;}}}' $FILE_NAME) )  for each in "${alpha[@]}" do    awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME   done  }   Script : main.sh   #function called in main.sh script   demo demo.txt 'SAL|COL|PER|TAG'   issue is how do i get the output into an array   below is the output i am getting need to store into array1   3.57 40.68 6.52 6.88   second array is this below output getting from another program   array2={3.57 40.68 6.52 6.88}   **so that I can match the array1 with array 2 position value    array1[0] with array2[0]  array1[1] with array2[1]  array1[2] with array2[2]  array1[3] with array2[3]    if all succeed should display success status as YES  i have solution which is partially working ... help me with some solution  so that i can store each value of an array to array1   FILE_NAME="$1" COLUMN_NAME="$2"  alpha=( $(awk -F"|" 'NR==1{for(i=1;i&lt;=NF;i++){if ($i ~ /'$COLUMN_NAME'/){print i;}}}' $FILE_NAME) )  declare -a array=();  for each in "${alpha[@]}" do         #var=($(awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME))         mapfile -t array &lt;&lt;&lt; "$(awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME)"         #awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME done  echo "${array[@]}"
942
I use Ubuntu 18 and have little experience with Linux commands. I have two files:  file 1:  a,txt1,v1 b,txt2,v2 c,txt3,v1 d,txt4,v2   file2:  a,txt5,v2 b,txt6,v1 c,txt7,v1 d,txt8,v2   I want to print the 1st and 3rd column values where the the 1st column value is identical and the 3rd column value has changed from v1 to v2. So my expected output should be:  a   The second query is for the 3rd column values that has changed from v2 to v1. So my expected outcome:  b
38993
I am new to UNIX and am trying to learn basic level text processing in UNIX so maybe this question may seem very basic but I would really appreciate any guidance.  I have a text file as below  A 1 B 2 C 9 D 1 A 5 B 3 C 4 A 6 C 7 A 5 C 1   for which I was able to piece together a command with some help as below  cat | awk 'BEGIN{OFS=FS=" "}!(($1,$2)in c){c[$1,$2]=1;r[$1]=r[$1] OFS $2} END{for(i in r){print i r[i]}}'   inside a shell script to get it to the format below:  A 1 5 6 5 B 2 3 C 9 4 7 1 D 1   My shell script will also take an argument k  (=7 say)  In this case, my script should sample 1 element from each box (A,B,C,D) until it has a total of 7 elements sampled. This will be like follows: First pick 1 from A, then 2 from B, 9 from C, 1 from D, 5 from A, 3 from B, 4 from C and display  A 1 5 B 2 3 C 9 4 D 1   If instead I had k = 9 as my input, then my output should be  A 1 5 6 B 2 3 C 9 4 7 D 1
33412
I run a command and pipe it through awk as below :  mycommand |  awk '{print $1,tolower($3),$4}  This gives me the following output:  fred.o: t .abcdefg  fred.o: u .rstuvwxy  fred.o: t .defghi  fred.o: t .jklmnop  fred.o: d abcdefg  fred.o: d zyxwvuts    As you can see the sometimes the third column contains a leading "." but not always.  How can I modify my awk command to get rid of the leading "." in the third column if it exists ?  eg I would like my output to look like this :   fred.o: t abcdefg   fred.o: u rstuvwxy   fred.o: t defghi   fred.o: t jklmnop   fred.o: d abcdefg   fred.o: d zyxwvuts
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?
82863
How can I insert some text in specific lines of a file? What is the simplest method that I can use? (bash, sed, awk?) What I want to do might be simple, but I don't know where I should start. It takes too much time for me to try to do this manually (I have a lot of files that I have to change for Excel/Calc later use). Here is my input file example: 4.06 4.05 5.04 4.06 34.50 56.06 45.33 36.44  And I want something like this (insert text before line 1 and 5): Exp1     4.06 4.05 5.04 4.06 Exp2 34.50 56.06 45.33 36.44  How can I do this?
32189
I have a very large csv file. How would you remove the very last , with sed (or similar) ?   ... [11911,0,"BUILDER","2014-10-15","BUILDER",0,0], [11912,0,"BUILDER","2014-10-15","BUILDER",0,0], [11913,0,"BUILDER","2014-10-15","BUILDER",0,0], ]   Desired output  ... [11911,0,"BUILDER","2014-10-15","BUILDER",0,0], [11912,0,"BUILDER","2014-10-15","BUILDER",0,0], [11913,0,"BUILDER","2014-10-15","BUILDER",0,0] ]   The following sed command will delete the last occurrence per line, but I want per file.   sed -e 's/,$//' foo.csv   Nor does this work  sed '$s/,//' foo.csv
95955
I have a file that I would like to break up into multiple files with uniq values for the first column. For example, here is a file:  fileA.txt   1    Cat 1    Dog 1    Frog 2    Boy 2    Girl 3    Tree 3    Leaf 3    Branch 3    Trunk   I would like my output to look something like this:  file1.txt   1    Cat 2    Boy 3    Tree   file2.txt  1    Dog 2    Girl 3    Leaf   file3.txt  1    Frog 3    Branch   file4.txt  3    Trunk   If a value does not exist, I want it to be skipped. I have tried to search for similar situations to mine, but I've come up short. Does anyone have idea of how to do this?  Edit: My awk version is: awk version 20070501
9931
This one-liner removes duplicate lines from text input without pre-sorting.  For example:  $ cat &gt;f q w e w r $ awk '!a[$0]++' &lt;f q w e r $    The original code I have found on the internets read:  awk '!_[$0]++'  This was even more perplexing to me as I took _ to have a special meaning in awk, like in Perl, but it turned out to be just a name of an array.  Now, I understand the logic behind the one-liner:  each input line is used as a key in a hash array, thus, upon completion, the hash contains unique lines in the order of arrival.  What I would like to learn is how exactly this notation is interpreted by awk. E.g. what the bang sign (!) means and the other elements of this code snippet.  How does it work?
75539
I have a text file with tab-separated columns which I'd like to process using awk. Here's an example of such a file: size=1\tname=foo\tweight=1.2 weight=2.5\tname=bar\tsize=2  What I want to achieve is to normalize the numeric value in columns whose content is like $field_name=&lt;number&gt; to four decimal places and keep the rest as is. Here, $field_name is a shell variable that is passed to awk and I'd like to use its value inside a regex. Here's a snippet (which is not working of course). I'm particularly interested in fixing line #5 in the following awk script and not solutions using other tools, such as sed, perl, python, etc. $ cat &quot;${file}&quot; \                                       # 1     | awk -F &quot;\t&quot; -v field_name=&quot;${external_var}&quot; '     # 2       {                                                 # 3         for (i = 1; i &lt;= NF; i++) {                     # 4           if ($i ~ /$field_name=[0-9]*.?[0-9]+/) {      # 5             split($i, kv, &quot;=&quot;)                          # 6             $i = sprintf(&quot;%s=%.4f&quot;, kv[1], kv[2])       # 7           }                                             # 8         }                                               # 9         print $0                                        # 10       }'
59495
I have a issue since I'm not familiar with awk. I have csv file generated from sar -d output converted to csv style:  12:33:41        unix,restarts 12:35:00,lofi4096,0,0.0,0,0,0.0,0.0 ,iscsi0,0,0.0,0,0,0.0,0.0 ,scsi_vhc,0,0.0,0,0,0.0,0.0 ,nfs1,0,0.0,0,0,0.0,0.0  12:45:00,lofi4096,0,0.0,0,0,0.0,0.0 ,iscsi0,0,0.0,0,0,0.0,0.0 ,scsi_vhc,0,0.0,0,0,0.0,0.0 ,nfs1,0,0.0,0,0,0.0,0.0   and I want to convert to this  12:33:41        unix,restarts 12:35:00,lofi4096,0,0.0,0,0,0.0,0.0 12:35:00,iscsi0,0,0.0,0,0,0.0,0.0 12:35:00,scsi_vhc,0,0.0,0,0,0.0,0.0 12:35:00,nfs1,0,0.0,0,0,0.0,0.0  12:45:00,lofi4096,0,0.0,0,0,0.0,0.0 12:45:00,iscsi0,0,0.0,0,0,0.0,0.0 12:45:00,scsi_vhc,0,0.0,0,0,0.0,0.0 12:45:00,nfs1,0,0.0,0,0,0.0,0.0   My attempt but since awk reads line by line I don't know out to keep the previous line value. I would expect that what I have should work. Any idea how I can obtain my desired result. I tried with awk, but I guess this should be possible with sed or the hard way with a custom shell script ( I'm trying to avoid doing this part ).  #!/usr/bin/awk -f BEGIN {         FS=","; } {         print $1         if ($1 != "") {                 mydate=$1;                 print $0;         }         else {                 print $mydate","$0;         } }   Running the system with Solaris 11.1.
63575
I want to remove last character from a line:  [root@ozzesh ~]#df -h | awk  '{ print $5 }' Use% 22% 1% 1% 59% 51% 63% 5%  Expected result:  Use 22 1 1 59 51 63 5
25813
I have two files.  I want to match the 1st column of first file to 1st column of second file. If there is a match I'd like to print the 2st column of second file, if not 0.  I have tried awk (awk 'FNR==NR{a[$1]=$0;next} {print a[$1]}' 2.txt 1.txt) but it is not working.  F1.txt  A B C D E F G H I J   F2.txt  A  0.5 E   1 H   0.5 J    1   Desired_output  A    0.5 B    0 C    0 D    0 E    1 F    0 G    0 H    0.5 I    0 J   1
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
741
How can I do the following to a CSV file using sed or awk?   Delete a column Duplicate a column Move a column   I have a big table with over 200 rows, and I'm not that familiar with sed.
51649
I have a file with a table of numbers; the first line is a header line. I want to sum each column and print out sum and column name for each column using awk.  How can I do it?