Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i have a data file with comma (,) seperated values. e.g: yash,yadav,ft,hyd,bang
now i need to remove 1st 2 entries in this line. i.e. output should be like: ft,hyd,bang
any help will be appreciated..

I'm assuming you want a unix shell script. My OS is Solaris, so I'm using nawk instead of awk:
#!/bin/kshnawk -F"," ' {
for(i=3; i<NF; i++)
printf("%s,", $i)
printf("%s\n", $NF)}' datafile.txt

Mine is SUN-OS..
the actual problem is 'i hav a string in CSV (comma Seperated Format). i need to remove 1st 7 entries along with thr commas and last 3 entries wid thr commas.'
please advice me on this... asap...

i found the cmd which can do this work, nt thr are limitations... hw to overcome these...?
the cmd is: sed 's/'`tail -l temp.txt|awk -F"," '{print $3}'`,'//g' temp.txt >> temp.txt
this wld remove the 3rd entry from temp.txt, bt the limitations are:
1) i'm nt able to apply loop on this, since i need to remove 1st 7 entries..
2) if any entry has "space" in between the entry, its nt working....
nails.... any help on this....????

one work around would be replace all spaces by any other special character. bt any other better solution.... ????

I do not know what you are trying to do with the sed command, and I do not have time to decipher it. sed is not the best command when you are dealing with fields - awk (or perl,python,ruby or other scripting language) is.
From your spec, I'm assuming that you want to keep fields 8 to the number of fields, NF-3. The NF-3 skips the last 3 fields:
So, if data file contains:
1,2,3,4,5,6,7,8,9,10,11,12,13
your output should be:
8,9,10
Here is my mod to my previous awk script:
#!/bin/kshnawk -F"," ' {
myrec=NF-3
for(i=8; i<myrec; i++)
printf("%s,", $i)
printf("%s\n", $myrec)}' datafile

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |