0

I have a file .csv and would like to add information to it. The delimiter is ; .
I had an idea to do so but it didn't really work. It was :

    cat file.csv | awk -F ";" '{ print ""$1" "$2" "$3 }' > temp.csv    
    cat temp.csv | while read info1 info2 info3
    do
         read -p 'Give another information :  " INFO
         echo "$info1 ; $info2 ; $info3 ; INFO" >> new_file.csv
    done

Everything works except "read -p" within the "while"... I was wondering if I should try only using an awk command but I actually don't really master this command so....Maybe someone has an idea to help me with that problem.

oz380
  • 57
  • 3
  • 9

3 Answers3

1
awk '
BEGIN{FS=OFS=";"}
{printf "Enter new stuff for line : " $0 "\n"; 
getline newstuff < "-"; 
print $0,newstuff > "newfile" }' file

Test:

$ cat file
this;is;my;line;one
this;is;my;line;two
this;is;my;line;three

$ awk 'BEGIN{FS=OFS=";"}{printf "Enter new stuff for line : " $0 "\n"; getline newstuff < "-"; print $0,newstuff > "newfile" }' file
Enter new stuff for line : this;is;my;line;one
hola
Enter new stuff for line : this;is;my;line;two
hello
Enter new stuff for line : this;is;my;line;three
bonjour

$ cat newfile
this;is;my;line;one;hola
this;is;my;line;two;hello
this;is;my;line;three;bonjour
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • I don't really understand how to put those lines in the code. I'd like to have the user enter the new information for each line. The result of the code would be a file with the old datas and the new ones. – oz380 Jun 27 '13 at 15:31
0

Entirely within awk:

awk 'BEGIN{FS=OFS=";"}{printf "prompt? ";getline info <"/dev/tty";print $1,$2,$3,info > "output"}'
Kevin
  • 53,822
  • 15
  • 101
  • 132
  • It works pretty well, but I'd like to create a new file with those lines. Is it possible to do that in within awk ? Something that would do sthg like this : print $1,$2,$3,info >> newfile.csv . – oz380 Jun 27 '13 at 15:27
  • Oh yes, I had meant to add that. Yes, it's what you typed but just one `>` (it's kept open through the whole file, so it doesn't clobber itself every line). I've edited it in. – Kevin Jun 27 '13 at 15:42
0

Do not try to do this all in awk, as an solution like that would be error prone and/or unnecessarily complicated.

Try this bash script:

> new_file
exec 3<file
while IFS=';' read -r f1 f2 f3 rest
do
        read -p "other info? " info <&1
        echo "$f1;$f2;$f3;$info" >> new_file
done <&3

e.g.:

$ > new_file
exec 3<file
while IFS=';' read -r f1 f2 f3 rest
do
        read -p "other info? " info <&1
        echo "$f1;$f2;$f3;$info" >> new_file
done <&3
other info? line 1
other info? line 2
other info? line 3

$ cat file
1;2;3;4
5;6;7;8
9;10;11;1

$ cat new_file
1;2;3;line 1
5;6;7;line 2
9;10;11;line 3
Ed Morton
  • 188,023
  • 17
  • 78
  • 185