Tag Info

New answers tagged

0

sed 's/||/| |/g;s/||/| |/g' filename-here The above command is for pipe so replace it with tabspace. You just need to replace empty columns with a blank space and pipe the output to the command you are already using.


1

Using awk: awk -F, '$9!~/Type 0/{print $0}' file AM_AUTO_1,1cx0,A166,B,C118,B,G129,B,A-minor Type I ACG AM_AUTO_2,1cx0,A165,B,C119,B,G128,B,A-minor Type II ACG AM_AUTO_3,1cx0,A166,B,C119,B,G128,B,A-minor Type II ACG AM_AUTO_4,1ddy,A25,A,C21,A,G8,A,A-minor Type I ACG Explanation: This causes awk to use , as delimiter (-F,). If the 9th field doesn't ...


1

With awk, you can do: $ awk '$(NF-1)!=0' file AM_AUTO_1,1cx0,A166,B,C118,B,G129,B,A-minor Type I ACG AM_AUTO_2,1cx0,A165,B,C119,B,G128,B,A-minor Type II ACG AM_AUTO_3,1cx0,A166,B,C119,B,G128,B,A-minor Type II ACG AM_AUTO_4,1ddy,A25,A,C21,A,G8,A,A-minor Type I ACG or using grep: grep -v 'Type 0' file


0

An awk solution awk -vq='"' ' func csv2del(n) { for(i=n; i<=c; i++) {if(i%2 == 1) gsub(/,/, OFS, a[i]) else a[i] = (q a[i] q) out = (out) ? out a[i] : a[i]} return out} {c=split($0, a, q); out=X; if(a[1]) $0=csv2del(1) else $0=csv2del(2)}1' OFS='|' file


0

Probably a qsv would be more your speed? sed 's/.*/,&,/;:t s/,"/"/;s/",/"/;s/,\([^"]*\),/"\1/;tt ' <<\DATA "prabhat,kumar",19,2000,"bangalore,India" DATA OUTPUT "prabhat,kumar"19"2000"bangalore,India"


1

Ruby is handy for CSV parsing: ruby -rcsv -ne 'puts CSV.generate_line(CSV.parse_line($_), :col_sep=>"|")' file prabhat,kumar|19|2000|bangalore,India|ABC,DEF|GHI|123|KLM|NOP,QRS prabhat,kumar|19|2000|bangalore,India|ABC,DEF|GHI|123|KLM|NOP,QRS Note that there are no quotes in the outputs. This is because none of the fields contain the field ...


0

This worked for me: $ echo '"prabhat,kumar",19,2000,"bangalore,India"' | awk -F, '{print $1,$2,$3,$4,$5,$6}'| awk -F\" '{print $2,$3,$4}'|awk -F\ '{print $1","$2,$3,$4,$5","$6}'`


2

If you have GNU awk: $ awk -vFPAT='[^,]*|"[^"]*"' '{ gsub("^\"|\"$","",$1); gsub("^\"|\"$","",$4); print $1 $4} ' prabhat,kumarbangalore,India The output format is a little ugly as I've only printed $1 and $4 next to each other - I'm sure you can alter it to your taste. If you need to retain the double quotes around the fields, remove both gsub(); ...


5

First of all, you should use a proper CSV parser. For example, in Perl, you can use Text::CSV: Install cpanm (if you use Perl, you'll thank me later) $ sudo apt-get install cpanminus If you're not on a Debian based system, you should be able to install it using your distribution's package manager. Install the Text::CSV module $ sudo cpanm Text::CSV ...


0

You can use perl instead: $ echo '"prabhat,kumar",19,2000,"bangalore,India"' | perl -F',(?![[:alpha:]])' -anle 'print "$F[0] $F[3]"' "prabhat,kumar" "bangalore,India"


1

the same in awk, with a head in first 2 lines, extracting the third field, assuming filed separator is ',' (comma). awk -F, 'NR>2 { print $3 ; }' *.csv


1

To extract from the 3rd column for example, you can do: cut -d',' -f3 OCS_mobfwref-oam_d01_2014080* This only works if there are no un-escaped comma's! If you want to remove the headers, do: sed 1d OCS_mobfwref-oam_d01_2014080* | OCS_mobfwref-oam_d01_2014080*


0

sed 's/||/| |/g;s/||/| |/g' filename-here | column -s"|" -t | less -#2 -N -S Explanation: Sed will add a space between blank delimters Column will add equal spacing between the columns zydsld|asl|asd das|aosdk|dd becomes zydsld|asl |asd das |aosdk|dd Less will open the output in a file viewer. -N and -S will add line number and disable wrapping ...


0

If all the blocks have exactly the same format (same field names, in the same order), then you can use awk in “paragraph mode”, and print the desired field number in each block. If there are always spaces around the equal signs and the values never contain spaces, you can rely on whitespace-separated fields. awk -v RS= -v ORS=',' '{print $3, $6, $9, $12, ...


9

Simply: awk -v RS= -v OFS=, '{print $3,$6,$9,$12,$15,$18}' An empty record separator (RS=) enables the paragraph mode whereby records are separated by sequences of empty lines. Inside a record, the default field separator applies (records are separated by blanks) so in each record, the fields we are interested in are the 3rd, 6th, 9th... We change the ...


3

Save the following to a file (eg split.awk) BEGIN { RS="\n\n"; FS="\n"; ORS=","; } { for (i=1;i<=NF;i++) { split($i, sf, "= ") print sf[2] } printf "\n" } Then run: awk -f split.awk Test.txt Or run the whole command as one line: awk 'BEGIN {RS="\n\n";FS="\n";ORS=",";}{for(i=1;i<=NF;i++){split($i, sf, "= ")print ...


4

A bash solution: declare -a out EOF=false IFS=$'=' until $EOF do ...


-1

Bash in combination with sed: #!/bin/bash cnt=0; while IFS=, read a b c; do echo -n $a ; cnt=$[cnt+1]; sed -e 's/[^,]*,\([^,]*\).*/\1/' file | sed -e 's/\([[:upper:]]\)//g'| sed -e $cnt'q;d'| tr '\n' ' ' ; echo $c; done < file produces: R1C1 123abc R1C3 R2C1 456defg R2C3 R3C1 789hijkl R3C3


2

if the second field has capitals scattered, awk -F ',' '{gsub(/[[:upper:]]/, X, $2)}1' OFS=, file


2

You can use awk: $ awk -F',' '{sub(/[[:upper:]]+/,"",$2)}1' OFS=',' file R1C1, 123abc, R1C3 R2C1, 456defg, R2C3 R3C1, 789hijkl, R3C3



Top 50 recent answers are included