10

I have a csv file with data presented as follows

87540221|1356438283301|1356438284971|1356438292151697
87540258|1356438283301|1356438284971|1356438292151697
87549647|1356438283301|1356438284971|1356438292151697

I'm trying to save the first column to a new file (without field separator , and then delete the first column from the main csv file along with the first field separator.

Any ideas?

This is what I have tried so far

awk 'BEGIN{FS=OFS="|"}{$1="";sub("|,"")}1'

but it doesn't work

Deano
  • 11,582
  • 18
  • 69
  • 119

5 Answers5

18

This is simple with cut:

$ cut -d'|' -f1 infile
87540221
87540258
87549647

$ cut -d'|' -f2- infile
1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697

Just redirect into the file you want:

$ cut -d'|' -f1 infile > outfile1

$ cut -d'|' -f2- infile > outfile2 && mv outfile2 file 
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
7

Assuming your original CSV file is named "orig.csv":

awk -F'|' '{print $1 > "newfile"; sub(/^[^|]+\|/,"")}1' orig.csv > tmp && mv tmp orig.csv
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
3

GNU awk

awk '{$1="";$0=$0;$1=$1}1' FPAT='[^|]+' OFS='|'

Output

1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697
Zombo
  • 1
  • 62
  • 391
  • 407
  • 1
    How does it work? – darw Aug 24 '22 at 14:28
  • @darw Sets `$1` null. `$0=$0` resplits the record, which now starts with a `|` (the OFS). FPAT doesn't match a null, so `$1` is now the (former) second field. `$1=$1` causes a rebuild of the output record, with `OFS='|'`. The trailing `{...}1` is a shorthand for print: It is the pattern of a second pattern-action pair, and is boolean "true". With no action, awk defaults to `{print}`. gawk recognises variable settings after the program text without needing a `-v`. – Douglas Royds Aug 11 '23 at 07:25
1

Pipe is special regex symbol and sub function expectes you to pass a regex. Correct awk command should be this:

awk 'BEGIN {FS=OFS="|"} {$1=""; sub(/\|/, "")}'1 file

OUTPUT:

1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

With sed :

sed 's/[^|]*|//' file.txt
rook
  • 5,880
  • 4
  • 39
  • 51