LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-21-2010, 08:47 AM   #1
btacuso
Member
 
Registered: May 2009
Posts: 32

Rep: Reputation: 15
How can I extract strings based on character positions?


I have a file with no regular way to grep or sed for any regular expression. The wordings, fields, spacings are random.

myfile:
asdjfvj jd sdfadjf dfafas asfafsaf
fdgjldkfg sdgfdg sfjhgfds h hgfh s sh fd
fj vv kzvh lkzv vzlb ffa.

I want to extract character positions 3 to 10 including spaces.

desired result:
djfvj j
gjldkfg
vv kz

Thank you very much.
 
Old 03-21-2010, 09:03 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Code:
# cat myfile | awk '{print substr($0,3,7)}'
djfvj j
gjldkfg
 vv kzv
 
1 members found this post helpful.
Old 03-21-2010, 09:38 AM   #3
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Cut works too:

Code:
bash-3.1$ cut -c 3-9 test
djfvj j
gjldkfg
 vv kzv
Or

Code:
bash-3.1$ cut -c 3-10 test
djfvj jd
gjldkfg 
 vv kzvh
whichever characters you need, it looks like your example is wrong.

Last edited by H_TeXMeX_H; 03-21-2010 at 10:07 AM.
 
1 members found this post helpful.
Old 03-21-2010, 01:14 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
And while 'cut' is certainly the most sensible tool for this
job I'd like to add a sed example :}
Code:
$    echo "asdjfvj jd sdfadjf dfafas asfafsaf
> > fdgjldkfg sdgfdg sfjhgfds h hgfh s sh fd
> > fj vv kzvh lkzv vzlb ffa." | sed -r 's/^...(.{7}).*/\1/'
jfvj jd
dgjldkf
j vv kz

Cheers,
Tink
 
1 members found this post helpful.
Old 03-21-2010, 01:20 PM   #5
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by H_TeXMeX_H View Post
Cut works too:

Code:
bash-3.1$ cut -c 3-9 test
djfvj j
gjldkfg
 vv kzv
Or

Code:
bash-3.1$ cut -c 3-10 test
djfvj jd
gjldkfg 
 vv kzvh
whichever characters you need, it looks like your example is wrong.
I was about to put a note about it. The example is moved automatically leftwards when I submitted it, but all you guys gave me the answer. Thanks again. BTW how do I thank each of you using the option by this forum? I tried and still it showed I thanked nobody.
 
Old 03-21-2010, 01:31 PM   #6
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by troop View Post
Code:
# cat myfile | awk '{print substr($0,3,7)}'
djfvj j
gjldkfg
 vv kzv
I tested all the posts. It all worked but this seems to be the one I have to use. Thanks again.
 
Old 03-21-2010, 01:38 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by btacuso View Post
I tested all the posts. It all worked but this seems to be the one I have to use. Thanks again.
I'm curious - WHY? :}

In terms of execution speeds/memory sizes:
cut > sed > awk

Why did you pick the slowest of the tools?


Cheers,
Tink
 
Old 03-25-2010, 09:22 AM   #8
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
I'm curious - WHY? :}

In terms of execution speeds/memory sizes:
cut > sed > awk

Why did you pick the slowest of the tools?


Cheers,
Tink
Sorry for not responding sooner. I picked the slowest one because it is in awk. I started my script in awk and I prefer not to mix other commands unless necessary. The speed is not noticeable though because I have only about 200-300 lines of input.
 
Old 03-25-2010, 12:31 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Fair enough, and no worries. Thanks for coming back!
 
  


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
Extract lines containing some strings without affectting sequential order cgcamal Programming 7 11-06-2008 11:57 PM
Using sed to extract a pattern plus a number of positions after roach7711x Linux - Software 20 10-31-2008 04:37 AM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
Helix seems to die when running Extract Strings abefroman Linux - Security 0 08-04-2005 09:30 AM


All times are GMT -5. The time now is 09:32 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration