Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
04-10-2007, 04:52 AM
|
#1
|
Member
Registered: Dec 2006
Posts: 362
Rep:
|
how can i truncate this
hi all
i have an output like this
Code:
ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123
ID=123:one=aa:two=aa@yahoo.com:three=:four=1111111111:five=
:/
i would like to truncate this write in a file (file.txt) as
Code:
//begin
a
a@yahoo.com
789
123
//begin
aa
aa@yahoo.com
11111111
//begin
how can do this using c program
can you please help em
thank you in advance
|
|
|
04-10-2007, 05:17 AM
|
#2
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Why not use something better suited to this sort of thing? Perl perhaps?
|
|
|
04-10-2007, 05:36 AM
|
#3
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
Code:
echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" |awk -F"=" '{ for( i=2; i<=NF; i+=2 ) { print $i } }'
|
|
|
04-10-2007, 05:41 AM
|
#4
|
Member
Registered: Dec 2006
Posts: 362
Original Poster
Rep:
|
thank you for quick replay
Quote:
Originally Posted by matthewg42
Why not use something better suited to this sort of thing? Perl perhaps?
|
no. my total code is in c so, i need help in c
can u help me
thank you in advance
|
|
|
04-10-2007, 05:59 AM
|
#5
|
Member
Registered: Dec 2006
Posts: 362
Original Poster
Rep:
|
thank you for quick replay
Quote:
Originally Posted by kshkid
Code:
echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" |awk -F"=" '{ for( i=2; i<=NF; i+=2 ) { print $i } }'
|
i tried
this
Code:
[root@localhost 1234]# echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" |awk -F"=" '{ for( i=2; i<=NF; i+=2 ) { print $i } }'
123:one
a@yahoo.com:three
:five
[root@localhost 1234]#
it has print in file like
Code:
//begin
a
a@yahoo.com
789
123
//begin
aa
aa@yahoo.com
11111111
can you help me
thank you in advance
|
|
|
04-10-2007, 06:03 AM
|
#6
|
Member
Registered: Mar 2006
Posts: 627
Rep:
|
Are you reading the output from a file or is your code producing that and you need to read it and then convert. Either way you could use strtok function to strip and then strchr to find the data after =
Last edited by knobby67; 04-10-2007 at 06:06 AM.
|
|
|
04-10-2007, 06:06 AM
|
#7
|
Member
Registered: Aug 2006
Location: Aberdeen, Scotland
Distribution: OpenBSD
Posts: 164
Rep:
|
Quote:
Originally Posted by munna_dude
no. my total code is in c so, i need help in c
can u help me
thank you in advance
|
So can you write C? If so read input until you reach an = then copy the input back onto standardout until you reach a :, and print a newling when you reach it. I don't think many people will write it for you, but if i've understood thats all what you have to do.
|
|
|
04-10-2007, 06:25 AM
|
#8
|
Member
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462
Rep:
|
Or maybe use strsep(),but read the manpage carefully.
I'm not writing it :0)
I hate parsin strings in C.
Shouldn't be too hard though if you apply yourself.
:0)
|
|
|
04-10-2007, 07:16 AM
|
#9
|
Member
Registered: Dec 2006
Posts: 362
Original Poster
Rep:
|
thank you for quick replay
Quote:
Originally Posted by kshkid
Code:
echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" |awk -F"=" '{ for( i=2; i<=NF; i+=2 ) { print $i } }'
|
this also nice but it is not worlikg
let me know how can i do like this.
it giving output like this
[root@localhost 1234]# echo "ID=123  ne=a:two=a@yahoo.com:three=789:four=:five=123" |awk -F"=" '{ for( i=2; i<=NF; i+=2 ) { print $i } }'
123  ne
a@yahoo.com:three
:five
[root@localhost 1234]#
but i would like to print and write in to a fine like
//begin
a
a@yahoo.com
789
123
//begin
aa
aa@yahoo.com
11111111
please help me in this way
thank you in advance
|
|
|
04-10-2007, 08:25 AM
|
#10
|
Member
Registered: Dec 2006
Posts: 362
Original Poster
Rep:
|
thank you for quick replay
sorry.
i forgot some code.
actually this is the response
|
|
|
04-10-2007, 08:26 AM
|
#11
|
Member
Registered: Dec 2006
Posts: 362
Original Poster
Rep:
|
thank you for quick replay
Quote:
Originally Posted by munna_dude
sorry.
i forgot some code.
actually this is the response
|
Code:
char *response="LIST=SUCCESS:ID=123:one=a:two=a@yahoo.com:three=:four=456:five=666\nID=123:one=aa:two=aa@yahoo.com:three=22222222:four=:five=0000000000\n:/";
there is an extra ":" in first line so,
the first "five"(i.e. 666) is not printing.
can you please help me in this way
thank you in advance
|
|
|
04-10-2007, 08:56 AM
|
#12
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
try this one!
Code:
echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" | sed 's/\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):.*$/\/\/begin\
\2\
\4\
\6/'
|
|
|
04-10-2007, 06:35 PM
|
#13
|
Member
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Rep:
|
What about some shell scripting? (ok, no C/C++ today).
Code:
#!/bin/bash
# reads from "file" and outputs to "file2"
exec 6>&1 ; exec > file2
IFS=":"
while read line ; do
# assigning with <<< seems to have portability problems.
# read -r -a line_array <<< "${line}"
line_array=( ${line} )
echo '//begin'
eval ${line_array[*]}
for number in one two three four five ; do
eval echo "$"$number
done
done < file
# maybe an additional...
echo '//begin'
exec 1>&6 6>&-
Last edited by omnio; 04-10-2007 at 06:41 PM.
|
|
|
04-11-2007, 12:05 AM
|
#14
|
Member
Registered: Dec 2006
Posts: 362
Original Poster
Rep:
|
thank you for quick replay
Quote:
Originally Posted by kshkid
try this one!
Code:
echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" | sed 's/\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):.*$/\/\/begin\
\2\
\4\
\6/'
|
the out put is
[root@localhost giis4.1]# echo "ID=123  ne=a:two=a@yahoo.com:three=789:four=:five=123" | sed 's/\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):.*$/\/\/begin\
> \2\
> \4\
> \6/'
//begin
a
a@yahoo.com
789
[root@localhost giis4.1]#
i would like to print all the fives
four has no number for that blank space have to come.
the above was printing only one,two,three.
if some lines have grater than five means(one extra char *six)
wt can i do?
can you please help me.
thank you in advance
|
|
|
04-11-2007, 02:41 AM
|
#15
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
I exactly made use of your sample output to arrive at the command required and that is what I had provided.
Am sorry that it is not clear!
Could you please post,
the exact output as how it is needed against a sample input ?

|
|
|
All times are GMT -5. The time now is 12:31 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|