LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-10-2007, 04:52 AM   #1
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Rep: Reputation: 30
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
 
Old 04-10-2007, 05:17 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Why not use something better suited to this sort of thing? Perl perhaps?
 
Old 04-10-2007, 05:36 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
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 } }'
 
Old 04-10-2007, 05:41 AM   #4
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
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
 
Old 04-10-2007, 05:59 AM   #5
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
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
 
Old 04-10-2007, 06:03 AM   #6
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
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.
 
Old 04-10-2007, 06:06 AM   #7
Indiestory
Member
 
Registered: Aug 2006
Location: Aberdeen, Scotland
Distribution: OpenBSD
Posts: 164
Blog Entries: 1

Rep: Reputation: 30
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.
 
Old 04-10-2007, 06:25 AM   #8
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
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)
 
Old 04-10-2007, 07:16 AM   #9
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
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=123ne=a:two=a@yahoo.com:three=789:four=:five=123" |awk -F"=" '{ for( i=2; i<=NF; i+=2 ) { print $i } }'
123ne
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
 
Old 04-10-2007, 08:25 AM   #10
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
thank you for quick replay

sorry.
i forgot some code.
actually this is the response
 
Old 04-10-2007, 08:26 AM   #11
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
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
 
Old 04-10-2007, 08:56 AM   #12
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
try this one!


Code:
echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" | sed 's/\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):.*$/\/\/begin\
\2\
\4\
\6/'
 
Old 04-10-2007, 06:35 PM   #13
omnio
Member
 
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16
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.
 
Old 04-11-2007, 12:05 AM   #14
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
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=123ne=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
 
Old 04-11-2007, 02:41 AM   #15
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
//begin
a
a@yahoo.com
789

123
//begin
aa
aa@yahoo.com

11111111

//begin

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 ?

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Truncate A File Dovetails Linux - Newbie 2 10-22-2005 06:58 PM
truncate lines with c alaios Programming 17 08-25-2005 08:51 AM
truncate the first character alaios Linux - General 1 02-10-2005 04:55 AM
how to use truncate sureshbabu Linux - Newbie 1 10-29-2003 04:22 AM
truncate command butthead Linux - General 1 05-14-2002 03:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration