Tag Info

New answers tagged

0

cp test test1 awk 'NR==1{last=$NF-1}{print $0,$NF-last-1; last=$NF}' test1 > test This has helped DPortal=ECCN RemoteFile=4004_130122 0256 A02 141111 1535 29545214 11 114040 0 DPortal=ECCN RemoteFile=4004_130122 0256 A02 141111 1535 29544814 11 114041 0 DPortal=ECCN RemoteFile=4004_130122 0256 A02 141111 1535 29544914 11 114042 0 DPortal=ECCN ...


1

Try this one: awk 'NR==1{last=$NF-1}{print $0,$NF-last-1; last=$NF}' file At first row we set variable last to a value of last field minus 1 ($NF-1), just for start. Later on last takes just value of $NF from previous row.


1

Or awk: For white-space delimited: awk 'NR==1 { print; next} {print $1, $2, $3, $4, substr($5,1,5)}' For comma delimited: awk 'BEGIN {FS=","} NR==1 { print; next} {print $1, $2, $3, $4, substr($5,1,5)}' For slightly better CSV splitting and assuming GNU awk: awk -vFPAT='[^,]*|"[^"]*"' 'NR==1 { print; next} {print $1, $2, $3, $4, substr($5,1,5)}'


1

If file.csv looks like this: 123456789,123456789,123456789,123456789,123456789 123456789,123456789,123456789,123456789,223456789 123456789,123456789,123456789,123456789,323456789 123456789,123456789,123456789,123456789,423456789 awk -F, '{print $1","$2","$3","$4","substr($5,1,5) }' file.csv will output this: 123456789,123456789,123456789,123456789,12345 ...


1

If all the values are simple (no quotes and newlines in a value) and if they are comma separated, like: 123456789,123456789,123456789,123456789,123456789 and if there are always 5 such values on a line, you can use sed: sed '2,$s/\(.*\),\(.*\),\(.*\),\(.*\),\(.....\)\(.*\)/\1,\2,\3,\4,\5/' input The 2,$ assumes you have a header that is comma separated ...


3

If you have GNU awk (or an equivalent that supports the FIELDWIDTHS variable), and you don't mind the -- marker being right-justified within the field width, then you could do something like gawk -vFIELDWIDTHS="4 7 4 6 8 8 6 8 8 8 7 5 4" -vOFS= ' {for (i=1;i<=NF;i++) sub(" $","--",$i);}1 ' file viz. $ gawk -vFIELDWIDTHS="4 7 4 6 8 8 6 8 8 8 7 5 4" ...



Top 50 recent answers are included