Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String handling

Status
Not open for further replies.

ajayarora

Programmer
Jun 5, 2003
24
0
0
CA
Hi,

I've a file with fields sperated by ~, like
1~2~3~4
1~x~y~z

My requirement is remove the first field and create another file rest of the fields like
2~3~4
x~y~z

I don't want to use "cut" command as it would be slow as compare to awk script. Any help would be appreciated.

Thanks.
 
I guess...
awk 'BEGIN {FS=OFS="~"}{print $2,$3,$4}' file1 > file2
or
awk '{print substr($0,index($0,"~")+1)}' file1 > file2

but both are slower than...
cut -d'~' -f2- file1 > file2
 
Try
Code:
awk '{sub("[^~]*~","");print}' infile > outfile
 
nawk -F'~' -v OFS='~' '{print substr($0,match($0,$2));}' file.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for your help. Don't we have something like -f2- in awk. Actually, the first field in the original file is a file-id and on the basis of which I've to create a file.
like
1~2~3~4 will result in a file say abc containing 2~3~4 and
2~x~y~z will go to another file say pqr containing x~y~z .

Regards
 
Try something like this:
Code:
awk -F'~' '
BEGIN{out[1]=&quot;abc&quot;;out[2]=&quot;pqr&quot;} # and so on ..
{print substr($0,index($0,&quot;~&quot;)+1)>out[$1]}
' /path/to/file

Hope This Help
PH.
 
Its working now...thanks for your help.

Regards
Ajay.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top