LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 05-14-2012, 12:32 AM   #1
iffarrukh
LQ Newbie
 
Registered: May 2008
Posts: 18

Rep: Reputation: 0
substring using awk and sed ( binary )


Hi..
I got a problem and I am looking some help. I was using this command to remove @ from binary file and then getting substr and finally using printf formating it and putting into a file. I am trying to run this using new version of awk(3.1.7) and I am just getting 4 character while previously I was almost 8727( awk 3.1.3)
I know awk is not supposed to work with binaries but code is written year ago and I cannot change a lot..

$ cat TT120T0-inbound | sed -e "s/@@//g;s/@$//" | awk ' begin {} {x = substr($0,1,length($0) - 4); printf("%s", x); }' >> Final-data

Any help..

I have attached the data as well but its a txt file not binary file so might not work properly . but just for reference. Have a look at snapshot as well.

Thanks in advance


Regards
Sam
Attached Thumbnails
Click image for larger version

Name:	snapshot.jpg
Views:	21
Size:	44.1 KB
ID:	9661  
Attached Files
File Type: txt binary.txt (9.2 KB, 22 views)
 
Old 05-14-2012, 03:47 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by iffarrukh View Post
Hi..
I got a problem and I am looking some help. I was using this command to remove @ from binary file and then getting substr and finally using printf formating it and putting into a file. I am trying to run this using new version of awk(3.1.7) and I am just getting 4 character while previously I was almost 8727( awk 3.1.3)
I know awk is not supposed to work with binaries but code is written year ago and I cannot change a lot..

$ cat TT120T0-inbound | sed -e "s/@@//g;s/@$//" | awk ' begin {} {x = substr($0,1,length($0) - 4); printf("%s", x); }' >> Final-data
There is no need to use cat, awk and sed. The same can be done with a single awk or sed command:
Code:
sed -e "s/@@//g;s/@$//;s/.\{4\}$//" TT120T0-inbound # this does not remove the newlines, though
                                                    # I am not sure if that is what you want
# or

awk '{ gsub(/@@/,"",$0); sub(/@$/, "", $0); sub(/....$/,"",$0); printf $0 }' TT120T0-inbound
Also, the lines in your file are quite long. Some implementations of awk or sed might have a problem with that.

Quote:
Originally Posted by iffarrukh View Post
Hi..
I have attached the data as well but its a txt file not binary file so might not work properly . but just for reference. Have a look at snapshot as well.
Ok, then what is the difference between the binary and text versions?
 
1 members found this post helpful.
Old 05-14-2012, 04:47 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
and also wc -c <filename> works. you do not need cat <filename> | to use wc
 
Old 05-14-2012, 11:11 PM   #4
iffarrukh
LQ Newbie
 
Registered: May 2008
Posts: 18

Original Poster
Rep: Reputation: 0
Hi millgates


Thanks for your help.. Sed command is working as I was looking for..

Would you mind telling me how this is working as I am having a bit confusion abt it..

sed -e "s/@@//g;s/@$//;s/.\{4\}$//" TT120T0-inbound


Regards
Sam
 
Old 05-15-2012, 12:28 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
sed has 3 commands:
Code:
s/@@//g
s/@$//
s/.\{4\}$//
the first one substitutes @@ with nothing, so it removes @@ from everywhere.
the second will remove the @ from the end of the lines
this two was taken from your original post

the third removes 4 characters from the end of the line.
 
1 members found this post helpful.
Old 05-15-2012, 06:15 PM   #6
iffarrukh
LQ Newbie
 
Registered: May 2008
Posts: 18

Original Poster
Rep: Reputation: 0
Hi

Thanks all for your support and help

I was just curious about third part of sed.



Cheers
Sam
 
Old 05-15-2012, 06:25 PM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
I think you could combine the first 2 sed commands:
Code:
sed -r 's/(@@|@$)//g' file
 
1 members found this post helpful.
Old 05-15-2012, 11:22 PM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Happy with solution ... mark as SOLVED

If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.

I'm glad to help you
 
  


Reply



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
Extract substring using sed hweontey Linux - Newbie 1 02-22-2011 03:27 AM
awk substring command casperdaghost Linux - Newbie 1 07-31-2010 07:26 AM
Using Grep/Awk/Sed to get a substring from a command johnjust Programming 5 01-12-2010 08:02 PM
using awk substring function on a file in a bash script matt007 Programming 3 06-17-2008 08:17 PM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM

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

All times are GMT -5. The time now is 11:55 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