LinuxQuestions.org
Review your favorite Linux distribution.
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 06-23-2014, 04:09 AM   #1
songor
LQ Newbie
 
Registered: Jun 2014
Posts: 2

Rep: Reputation: Disabled
How to get the first 7 characters of the lines which contain...??


Hi All

I hope someone can help. Not enough in the subject. But my full question is:
How to get the first 7 characters of the lines which contain character 'D' in position 120 from a file ???

eg
2222222 jason... ...DXXY (D is position 120)
2222223 peter... ...DXXY
2222224 john.... ...OXXY

The output should be:
2222222
2222223

But not 2222224 because position 120 is 'O', not 'D'

Is it possible to do it in sed ?? Please help

Many many thanks
 
Old 06-23-2014, 04:45 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
First part:
Code:
grep '^.{119}D'
Second part:
Code:
cut -b1-7
Now merge these together.
 
1 members found this post helpful.
Old 06-23-2014, 06:55 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
For ease in testing I changed the "D in position 120" to "D in position 20."

With this InFile ...
Code:
yes,yes,90123456ABCDEFGHIJK
reject,89012345678D0DD000DDD
oui,oui,90123456abcDefghijklmn
... this awk ...
Code:
awk -F "" '{if ($20=="D") print substr($0,1,7)}' $InFile >$OutFile
... produced this OutFile ...
Code:
yes,yes
oui,oui
Daniel B. Martin
 
1 members found this post helpful.
Old 06-23-2014, 05:26 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by songor View Post
Is it possible to do it in sed ??
Yes, but it's not pretty.

For ease in testing I changed the "D in position 120" to "D in position 20."

With this InFile ...
Code:
yes,yes,90123456ABCDEFGHIJK
reject,89012345678D0DD000DDD
oui,oui,90123456abcDefghijklmn
... this sed ...
Code:
sed -e '/^.\{19\}D/!d' -e 's/\(^.\{7\}\)\(.*\)/\1/' $InFile >$OutFile
... produced this OutFile ...
Code:
yes,yes
oui,oui
This part: -e '/^.\{19\}D/!d' keeps lines with D in position 20, and then ...
this part: -e 's/\(^.\{7\}\)\(.*\)/\1/' preserves the first 7 characters of those lines.

In my opinion the awk solution (see previous post) is easier to write, easier to read.

Daniel B. Martin
 
Old 06-25-2014, 01:24 AM   #5
songor
LQ Newbie
 
Registered: Jun 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by danielbmartin View Post
For ease in testing I changed the "D in position 120" to "D in position 20."

With this InFile ...
Code:
yes,yes,90123456ABCDEFGHIJK
reject,89012345678D0DD000DDD
oui,oui,90123456abcDefghijklmn
... this awk ...
Code:
awk -F "" '{if ($20=="D") print substr($0,1,7)}' $InFile >$OutFile
... produced this OutFile ...
Code:
yes,yes
oui,oui
Daniel B. Martin

Wow, work great! Many thanks
 
Old 06-25-2014, 07:00 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
A cleaner sed solution

Quote:
Originally Posted by songor View Post
Is it possible to do it in sed ??
For ease in testing I changed the "D in position 120" to "D in position 20."

With this InFile ...
Code:
yes,yes,90123456ABCDEFGHIJK
reject,89012345678D0DD000DDD
oui,oui,90123456abcDefghijklmn
... this sed ...
Code:
sed -n 's/\(^.\{7\}\)\(.\{12\}D.*\)/\1/p' $InFile >$OutFile
... produced this OutFile ...
Code:
yes,yes
oui,oui
In my opinion the awk solution (see previous post) is easier to write, easier to read.

Daniel B. Martin
 
Old 06-28-2014, 01:19 AM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Extended regular expressions (-r option) allow one to omit most of backslashes (but are a GNU extension):
Code:
$ cat /tmp/infile
yes,yes,90123456ABCDEFGHIJK
reject,89012345678D0DD000DDD
oui,oui,90123456abcDefghijklmn

$ sed -nr 's/(^.{7})(.{12}D.*)/\1/p' /tmp/infile
yes,yes
oui,oui
 
1 members found this post helpful.
Old 06-28-2014, 09:09 AM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by firstfire View Post
Code:
sed -nr 's/(^.{7})(.{12}D.*)/\1/p' /tmp/infile
Thank you for this improvement. Your code is fewer keystrokes and easier to read. Nice!

Daniel B. Martin
 
Old 06-28-2014, 05:35 PM   #9
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

I just noticed that last pair of parentheses can be omitted too:
Code:
$ sed -nr 's/(^.{7}).{12}D.*/\1/p' /tmp/infile
yes,yes
oui,oui
 
  


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
characters in long lines disappear on xorg pzaprawa Slackware 20 07-01-2011 03:24 AM
[SOLVED] How to add characters and lines to file nobtiba Programming 12 02-13-2011 08:40 PM
[SOLVED] Remove all lines containing extended characters hattori.hanzo Programming 8 11-09-2010 06:24 AM
[SOLVED] replace some characters in the lines liuqiong7618 Programming 5 07-20-2010 02:21 AM
using sed to insert lines with special characters disorderly Linux - Software 26 04-20-2006 05:30 PM

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

All times are GMT -5. The time now is 10:26 AM.

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