Hi Gurus,
I have a file as below.
deptnm-appnm-code ------- s(deptnm-ocode-30-ddd)
s(deptnm-ocode-00-dum)
s(deptnm-appnm-ecode)
deptnm-appnm-code-dld ------- s(deptnm-on-rundt-run)
s(deptnm-appnm-ocodel-su)
s(deptnm-appnm-ecode-dld)
s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum ------- s(deptnm-on-rundt-bp)
I need to change the format as below output.
deptnm-appnm-code ,s(deptnm-ocode-30-ddd)
,s(deptnm-ocode-00-dum)
,s(deptnm-appnm-ecode)
deptnm-appnm-code-dld ,s(deptnm-on-rundt-run)
,s(deptnm-appnm-ocodel-su)
,s(deptnm-appnm-ecode-dld)
,s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum ,s(deptnm-on-rundt-bp)
I tried command:
awk '{if(NF<3)$3=$1} {print $1, $3}' OFS="," demo.txt
but I got below output
deptnm-appnm-code,s(deptnm-ocode-30-ddd)
s(deptnm-ocode-00-dum),s(deptnm-ocode-00-dum)
s(deptnm-appnm-ecode),s(deptnm-appnm-ecode)
deptnm-appnm-code-dld,s(deptnm-on-rundt-run)
s(deptnm-appnm-ocodel-su),s(deptnm-appnm-ocodel-su)
s(deptnm-appnm-ecode-dld),s(deptnm-appnm-ecode-dld)
s(deptnm-ocode-50-curcnt),s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum,s(deptnm-on-rundt-bp)
how could I fix this script to get expected result?
Thanks in advance
[~]$ cat file
deptnm-appnm-code ------- s(deptnm-ocode-30-ddd)
s(deptnm-ocode-00-dum)
s(deptnm-appnm-ecode)
deptnm-appnm-code-dld ------- s(deptnm-on-rundt-run)
s(deptnm-appnm-ocodel-su)
s(deptnm-appnm-ecode-dld)
s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum ------- s(deptnm-on-rundt-bp)
[~]$ sed -e 's/-/ /g; s/\(s.dept\)/,&/' file
deptnm appnm code ,s(deptnm ocode 30 ddd)
,s(deptnm ocode 00 dum)
,s(deptnm appnm ecode)
deptnm appnm code dld ,s(deptnm on rundt run)
,s(deptnm appnm ocodel su)
,s(deptnm appnm ecode dld)
,s(deptnm ocode 50 curcnt)
deptnm appnm code dum ,s(deptnm on rundt bp)
[~]$
1 Like
balajesuri:
[~]$ cat file
deptnm-appnm-code ------- s(deptnm-ocode-30-ddd)
s(deptnm-ocode-00-dum)
s(deptnm-appnm-ecode)
deptnm-appnm-code-dld ------- s(deptnm-on-rundt-run)
s(deptnm-appnm-ocodel-su)
s(deptnm-appnm-ecode-dld)
s(deptnm-ocode-50-curcnt)
deptnm-appnm-code-dum ------- s(deptnm-on-rundt-bp)
[~]$ sed -e 's/-/ /g; s/\(s.dept\)/,&/' file
deptnm appnm code ,s(deptnm ocode 30 ddd)
,s(deptnm ocode 00 dum)
,s(deptnm appnm ecode)
deptnm appnm code dld ,s(deptnm on rundt run)
,s(deptnm appnm ocodel su)
,s(deptnm appnm ecode dld)
,s(deptnm ocode 50 curcnt)
deptnm appnm code dum ,s(deptnm on rundt bp)
[~]$
this code works, thanks.
sorry, there is a small issue:
s/-/ /g
replaced the records as well
deptnm-ocode-30-ddd to deptnm on rundt bp
I fixed it with
-e 's/ -------//g
is there a regular expression to fix this?
Is it really OK for the hyphens in the 1st and last fields to be changed to spaces? Do, you want tabs in the output instead of spaces (as shown in your sample output)?
The following seems to produce the output you originally said you wanted:
awk '{ f1 = (NF > 1) ? $1 : ""
tabs = (length(f1) < 8) ? "\t\t\t" : (length(f1) < 16) ? "\t\t" : "\t"
printf("%s%s,%s\n", f1, tabs, $NF)
}' file
As always, if you want to try this on a Solaris/SunOS system, change awk
to /usr/xpg4/bin/awk
, /usr/xpg6bin/awk
, or nawk
.
1 Like
Is it really OK for the hyphens in the 1st and last fields to be changed to spaces? Do, you want tabs in the output instead of spaces (as shown in your sample output)?
The following seems to produce the output you originally said you wanted:
awk '{ f1 = (NF > 1) ? $1 : ""
tabs = (length(f1) < 8) ? "\t\t\t" : (length(f1) < 16) ? "\t\t" : "\t"
printf("%s%s,%s\n", f1, tabs, $NF)
}' file
As always, if you want to try this on a Solaris/SunOS system, change awk
to /usr/xpg4/bin/awk
, /usr/xpg6bin/awk
, or nawk
.
Hi Don,
As always, thank you very much for your help. this code works perfectly.
ongoto
January 9, 2015, 11:17pm
6
An example in Bash...just ignores the --------- field.
#!/bin/bash
file="./lineup.data"
while read fld1 fld2 fld3
do
if [[ ${fld1:0:1} == 's' ]]; then
printf "%-24s ,%s\n" " " "$fld1"
else
printf "%-24s ,%s\n" "$fld1" "$fld3"
fi
done < $file
# output
# ------
# deptnm-appnm-code ,s(deptnm-ocode-30-ddd)
# ,s(deptnm-ocode-00-dum)
# ,s(deptnm-appnm-ecode)
# deptnm-appnm-code-dld ,s(deptnm-on-rundt-run)
# ,s(deptnm-appnm-ocodel-su)
# ,s(deptnm-appnm-ecode-dld)
# ,s(deptnm-ocode-50-curcnt)
# deptnm-appnm-code-dum ,s(deptnm-on-rundt-bp)
1 Like