Mir S Mehdi
Mir S Mehdi

Reputation: 1530

Adding text to a CSV file through shell script

I can use the below command to write text to beginning of each line in the file

sed 's/^/Adding Text /' filename

But I want to skip the first line and add a another text to the first line alone at the beginning.

My current file:

Summary, Task Id, Project ID, Project
Test file for Project Task, T12345, P123456, Test

The Output I am looking for is:

Type, Summary, Task Id, Project ID, Project
Adding Text, Test1 file for Project Task, T12345, P123456, Test
Adding Text, Test1 file for Project Task, T12345, P123456, Test

Type should be added to the first line and Adding Text should be added to all other lines.

Upvotes: 1

Views: 2437

Answers (2)

Jotne
Jotne

Reputation: 41460

Here is one awk

awk '{print NR==1?"Type,":"Adding Text,",$0}' file

It test if line is number 1 and print one thing, if not print some other.

It may be some more robust using parentheses.

awk '{print (NR==1?"Type,":"Adding Text,"),$0}'
awk '{print (NR>1?"Adding Text,":"Type,"),$0}'

Upvotes: 0

ray
ray

Reputation: 4267

Try this one:

sed '1s/^/Type, /;2,$s/^/Adding Text, /' filename

1s/^Type, / : insert Type, at the beginning of 1st line
2,$s/^/Adding Text, / : starting from 2nd line, insert Adding Text, at the beginning of each line

or you can use awk

awk 'NR==1{print "Type,",$0}NR>1{print "Adding Text,",$0}' filename

Upvotes: 3

Related Questions