Reputation: 77
1) I have a Source_File.csv which looks like this
A;B;/Path/xx/file1_name
C;D;/anotherPath/yyyyy/file2_name
2) I want to produce a Target_File.csv
A;B;/Path/xx/;/Path/xx/file1_name
C;D;/anotherPath/yyyyy/;/anotherPath/yyyyy/file2_name
The command
cat Source_File.csv | awk -F";" '{print $1";"$2";"echo $3 |
cut -d'/' -f1,2,3";"$3}' >> Target_File.csv;
gives this error
awk: {print $1";"$2";"echo $3 | cut -d/ -f1,2,3";"$3}
awk: ^ syntax error
Thanks in advance for your Advices/Suggestions
Upvotes: 1
Views: 5754
Reputation: 784968
No need to use cat
, cut
etc. Just awk
alone is enough:
awk 'BEGIN{FS=OFS=";"} {s=$NF; sub(/[^\/]+$/, "", s); print $1,$2,s,$NF}' Source_File.csv
A;B;/Path/xx/;/Path/xx/file1_name
C;D;/anotherPath/yyyyy/;/anotherPath/yyyyy/file2_name
Upvotes: 3
Reputation: 29
What you are trying to do is extract the directory name and full file name as two separate components.
You can do this easily within awk using split() and arrays as -
cat /tmp/t.csv | awk -F";" '{ n=split($3, a, "/"); s1=""; i=1; while (i
A;B;//Path/xx;/Path/xx/file1_name C;D;//anotherPath/yyyyy;/anotherPath/yyyyy/file2_name
Easier would be to use just bash and do
cat /tmp/t.csv | sed -e 's/;/ /g' | while read a b c; do echo "$a;$b;dirname $c
;$c" ; done
Upvotes: -1
Reputation: 88583
With sed:
sed 's|/.*/|&;&|' file
Output:
A;B;/Path/xx/;/Path/xx/file1_name C;D;/anotherPath/yyyyy/;/anotherPath/yyyyy/file2_name
If you want to edit your file "in place" use sed's option -i
.
Upvotes: 2