LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How can I extract strings based on character positions? (http://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-extract-strings-based-on-character-positions-796847/)

btacuso 03-21-2010 09:47 AM

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.

troop 03-21-2010 10:03 AM

Code:

# cat myfile | awk '{print substr($0,3,7)}'
djfvj j
gjldkfg
 vv kzv


H_TeXMeX_H 03-21-2010 10:38 AM

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.

Tinkster 03-21-2010 02:14 PM

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

btacuso 03-21-2010 02:20 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 3906519)
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.

btacuso 03-21-2010 02:31 PM

Quote:

Originally Posted by troop (Post 3906485)
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.

Tinkster 03-21-2010 02:38 PM

Quote:

Originally Posted by btacuso (Post 3906712)
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

btacuso 03-25-2010 10:22 AM

Quote:

Originally Posted by Tinkster (Post 3906716)
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.

Tinkster 03-25-2010 01:31 PM

Fair enough, and no worries. Thanks for coming back!


All times are GMT -5. The time now is 09:34 AM.