Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix cut: Print same Field twice

Tags:

shell

unix

sed

cut

Say I have file - a.csv

ram,33,professional,doc
shaym,23,salaried,eng

Now I need this output (pls dont ask me why)

ram,doc,doc,
shayam,eng,eng,

I am using cut command

cut -d',' -f1,4,4 a.csv

But the output remains

ram,doc
shyam,eng

That means cut can only print a Field just one time. I need to print the same field twice or n times. Why do I need this ? (Optional to read) Ah. It's a long story. I have a file like this

#,#,-,-
#,#,#,#,#,#,#,-
#,#,#,-

I have to covert this to

#,#,-,-,-,-,-
#,#,#,#,#,#,#,-
#,#,#,-,-,-,-

Here each '#' and '-' refers to different numerical data. Thanks.

like image 726
ghosh'. Avatar asked Sep 13 '12 11:09

ghosh'.


People also ask

How do you cut the second field in Unix?

cut uses tab as a default field delimiter but can also work with other delimiter by using -d option. Note: Space is not considered as delimiter in UNIX.

How do I print two columns in Linux?

Printing two columnsThe pr command can print text in multiple columns. For example, -2 prints in two columns and -3 will print in three columns.


4 Answers

You can't print the same field twice. cut prints a selection of fields (or characters or bytes) in order. See Combining 2 different cut outputs in a single command? and Reorder fields/characters with cut command for some very similar requests.

The right tool to use here is awk, if your CSV doesn't have quotes around fields.

awk -F , -v OFS=, '{print $1, $4, $4}'

If you don't want to use awk (why? what strange system has cut and sed but no awk?), you can use sed (still assuming that your CSV doesn't have quotes around fields). Match the first four comma-separated fields and select the ones you want in the order you want.

sed -e 's/^\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\)/\1,\4,\4/'
like image 128
Gilles 'SO- stop being evil' Avatar answered Oct 19 '22 17:10

Gilles 'SO- stop being evil'


$ sed 's/,.*,/,/; s/\(,.*\)/\1\1,/' a.csv
ram,doc,doc,
shaym,eng,eng,

What this does:

  • Replace everything between the first and last comma with just a comma
  • Repeat the last ",something" part and tack on a comma. Voilà!

Assumptions made:

  • You want the first field, then twice the last field
  • No escaped commas within the first and last fields

Why do you need exactly this output? :-)

like image 40
Jens Avatar answered Oct 19 '22 17:10

Jens


using perl:

perl -F, -ane 'chomp($F[3]);$a=$F[0].",".$F[3].",".$F[3];print $a."\n"' your_file

using sed:

sed 's/\([^,]*\),.*,\(.*\)/\1,\2,\2/g' your_file
like image 1
Vijay Avatar answered Oct 19 '22 17:10

Vijay


As others have noted, cut doesn't support field repetition.

You can combine cut and sed, for example if the repeated element is at the end:

< a.csv cut -d, -f1,4 | sed 's/,[^,]*$/&&,/'

Output:

ram,doc,doc,
shaym,eng,eng,

Edit

To make the repetition variable, you could do something like this (assuming you have coreutils available):

n=10
rep=$(seq $n | sed 's:.*:\&:' | tr -d '\n')
< a.csv cut -d, -f1,4 | sed 's/,[^,]*$/'"$rep"',/'

Output:

ram,doc,doc,doc,doc,doc,doc,doc,doc,doc,doc,
shaym,eng,eng,eng,eng,eng,eng,eng,eng,eng,eng,
like image 1
Thor Avatar answered Oct 19 '22 18:10

Thor