Tag Info

New answers tagged

0

If the .csv is in DOS format another option might be: tr -d \\r <input.csv | sed 's/$/,new_col/' ...which would handle it all in one shot.


3

Your input csv file is in DOS format. Microsoft DOS/Windows use a Carriage Return (\r, ^M, ASCII code 13) followed by a Line Feed (\n, ^J or ASCII code 10) at the end of each line while Linux/Unix uses the Line feed only. To fix this, run: $ dos2unix file.csv dos2unix: converting file file.csv to Unix format ... $ awk '{print $0",new_col"}' file.csv ...


0

Would this work? awk -F, '{$(NF+1)="new_col";}1' OFS=, input.txt. This will output the new table in command line. In the example, I've added an output file (output.csv just in case). Before running the command: $ cat input.csv 68,110,0.362,0.101 11,98,0.405,0.101 57,94,0.374,0.100 61,123,0.342,0.100 124,130,0.298,0.100 Executing the command: $ awk -F, ...


0

Awk solution awk -F ',' '{split($4, a, "-"); $4 = (a[3] "/" a[2] "/" a[1])}1' OFS=, file


0

Another awk solution awk -F, -va="([0-9]+)" '$0=gensub((a"-"a"-"a),"\\3/\\2/\\1","g")' file -F, Sets field separator as comma -va="([0-9]+)" Sets a variable(a) as the regex in the quotes. ([0-9]+) This regex matches any number of number in the range of 0-9(so all numbers really) and is within brackets so the match is saved. $0=gensub Sets $0(the ...


0

With sed: $ sed -e 's#\([0-9]\{1,\}\)-\([0-9]\{1,\}\)-\([0-9]\{1,\}\)#\3/\2/\1#' file abc,124,123,13/08/2014,abc def,124,123,13/08/2014,abc ghi,124,123,13/08/2014,abc If you have GNU or FreeBSD sed, you can use: sed -E 's#([0-9]+)-([0-9]+)-([0-9]+)#\3/\2/\1#' file or perl: perl -pe 's#(\d+)-(\d+)-(\d+)#$3/$2/$1#'


0

Perl solution: perl -aF'[,-]' -ne 'print join ",", @F[0..2], join("/", @F[5,4,3]), $F[6]' < input


2

An awk solution: awk -F'[,-]' '{printf "%s,%s,%s,%s/%s/%s,%s\n", $1, $2, $3, $6, $5, $4, $7}' file Explanation: -F'[,-]: set the delimiter to , or - '{printf "%s,%s,%s,%s/%s/%s,%s\n", $1, $2, $3, $6, $5, $4, $7}': print the part in the desired order and a newline at the end. And a sed solution: sed 's|\([0-9]*\)-\([0-9]*\)-\([0-9]*\)|\3/\2/\1|g' file ...


3

If I understood well your question, it sounds like a typical join ("Join lines on a common field") use case : join --header -j 4 -t $'\t' file1.txt file2.txt You get 7 columns for each matching row. Here is what I get (for the slightly modified data, see below): Column_4 Column_1 Column_2 Column_3 Column_1 Column_2 Column_3 D2 A2 B2 ...


1

An awk solution: $ awk -F"\t" 'FNR==NR{a[$4];next} $4 in a' OFS="\t" file1 file2 Column_1 Column_2 Column_3 Column_4 Aa2 Bb2 Cc2 D2


2

There's a dedicated tool for that: paste. It concatenates each full line from the first file with the corresponding line from the second file; you can remove unwanted columns before or after. For example, assuming that your columns are tab-delimited: paste file1.txt file2.txt | cut -f 1,2,3,6 Here's a way to pre-filter both files that relies on ...


1

Try this: $ awk 'FNR==NR{a[FNR]=$2;next}{$NF=a[FNR]}1' file2 file1 A 23 8 0 A 63 9 6 B 45 3 5



Top 50 recent answers are included