Computing.Net > Forums > Unix > need help in scripting

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

need help in scripting

Reply to Message Icon

Name: yash_yadav1
Date: September 25, 2008 at 06:18:54 Pacific
OS: windows
CPU/Ram: NA
Product: NA
Comment:

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..



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 25, 2008 at 07:35:14 Pacific
Reply:

I'm assuming you want a unix shell script. My OS is Solaris, so I'm using nawk instead of awk:


#!/bin/ksh

nawk -F"," ' {
for(i=3; i<NF; i++)
printf("%s,", $i)
printf("%s\n", $NF)

}' datafile.txt



0

Response Number 2
Name: yash_yadav1
Date: September 26, 2008 at 02:26:53 Pacific
Reply:

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...


0

Response Number 3
Name: yash_yadav1
Date: September 26, 2008 at 05:17:36 Pacific
Reply:

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....????


0

Response Number 4
Name: yash_yadav1
Date: September 26, 2008 at 05:23:39 Pacific
Reply:

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


0

Response Number 5
Name: nails
Date: September 26, 2008 at 09:08:58 Pacific
Reply:

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/ksh

nawk -F"," ' {
myrec=NF-3
for(i=8; i<myrec; i++)
printf("%s,", $i)
printf("%s\n", $myrec)

}' datafile



0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: need help in scripting

need help in scripting ksh www.computing.net/answers/unix/need-help-in-scripting-ksh/7266.html

Replacing a word in a file www.computing.net/answers/unix/replacing-a-word-in-a-file-/7137.html

Need Help with KornShell script www.computing.net/answers/unix/need-help-with-kornshell-script/5978.html