2

I created a script that extracts data from a database and loads it to a csv file. I am using SunOS hstz1454 5.10 system. I am trying to do a sequence at the beginning of each line following with a commma.

Data looks like this below:

1,1,1535,0,1,Y,Y
1,1,1535,10,2,Y,Y
1,1,1535,20,1,Y,Y
1,1,1535,30,2,Y,Y
1,1,1535,40,1,Y,Y

I want my csv file to generate a sequence number at the beginning og each line follow by a comma:

1,1,1,1535,0,1,Y,Y
2,1,1,1535,10,2,Y,Y
3,1,1,1535,20,1,Y,Y
4,1,1,1535,30,2,Y,Y
5,1,1,1535,40,1,Y,Y

Here is my code only prints 1 and does not increment, how can I increment on each new line?

for fname in conv2016_02_cos_speed2percent_data_migration.csv
do
  echo "adds a seq number on each line for $fname"
  sed 's/.*/1,&/' $fname > tmp.tmp 
  mv tmp.tmp $fname
done
  • Are you using Linux or SunOS? Title metions linux, the question sunos/solaris... – chaos Dec 16 '15 at 21:53
  • I have SunOS hsdz0150 5.10 Generic_150400-20 sun4v sparc sun4v, I am new to shell scripting so I was not sure. – yesco1 Dec 16 '15 at 22:03
10

Use nl:

nl -s, -w1 file

This will add an incrementing number to the beginning of each line. -s, specifies that the number and the rest of the line should be separated by a comma. -w1 specifies the number of characters to be used for the line number.


Edit: As Stéphane Chazelas pointed out in the comment, the -w1 flag cuts the numbers with more than 1 digits. This may be a bug in Solaris' implementation of the nl utillity. Also, you may want to number special line content (-d $'\n\n') and emtpy lines (-ba):

NL='
'
nl -s, -ba -d "$NL$NL" file

The output (with GNU coreutils 8.12.197):

$ nl -s, -ba -d "$NL$NL" file
     1,4,2,64,1088,80,1,Y,Y
     2,5,2,64,1088,95,2,Y,Y
     3,8,2,1088,39813120,10,12,Y,Y
     4,6,2,1088,39813120,0,2,Y,Y
     5,7,2,1088,39813120,5,1,Y,Y
     6,8,2,1088,39813120,10,2,Y,Y

In your script it would then look like:

NL='
'
for fname in conv2016_02_cos_speed2percent_data_migration.csv; do
  nl -s, -ba -d "$NL$NL" "$fname"  > tmp.tmp && mv tmp.tmp "$fname"
done
  • To support arbitrary line content, with nl, you generally need to add -ba -d $'\n\n', otherwise it won't number empty lines or treat some special line content specially. – Stéphane Chazelas Dec 16 '15 at 21:55
  • @Chaos this only went from 1,2,3,4,5,6,7,8,9,0,1,2...and etc. It did not print 10,11,12 and so on – yesco1 Dec 16 '15 at 21:57
  • I'd recommend using mktemp to generate a guaranteed-unique temporary filename lest parallel invocations wipe out or corrupt data. – Andrew Henle Dec 16 '15 at 22:04
  • how do I do that? – yesco1 Dec 16 '15 at 22:07
  • 2
    @AndrewHenle, issue raised on the Austin group mailing list – Stéphane Chazelas Dec 16 '15 at 22:29
8

Here's a way with awk:

awk -v OFS=, '{print NR, $0}' < "$fname" > tmp && mv -- tmp "$fname"

and cat with sed

cat -n < "$fname" |
sed 's/^[[:blank:]]*\([[:digit:]]*\)[[:blank:]]*/\1,/' > tmp &&
mv -- tmp "$fname"

On Solaris, use /usr/xpg4/bin/sed|awk (or command -p sed|awk if using a POSIX shell or adapt $PATH to get a standard environment as detailed in standards(5)).

  • Hey people, upvote @chaos's answer – glenn jackman Dec 16 '15 at 21:50
  • @Stéphane, thanks for the reference to that man page. – glenn jackman Dec 17 '15 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.