The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extracting numbers from strings gobi Shell Programming and Scripting 2 05-26-2008 11:44 PM
Extracting lines between 2 strings only if the pattern patches simran Shell Programming and Scripting 5 04-23-2008 04:58 PM
extracting a set of strings from a text file Deanne Shell Programming and Scripting 2 09-20-2007 11:31 PM
Extracting the lines between 2 strings of a file babloo Shell Programming and Scripting 2 02-14-2007 11:27 AM
Extracting strings hugow UNIX for Dummies Questions & Answers 1 06-24-2005 06:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-12-2007
cmsdelhi cmsdelhi is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 1
Help with extracting strings from a file

I want to collect the characters from 1-10 and 20-30 from each line of the file and take them in a file in the following format.Can someone help me with this :



string1,string2
string1,string2
string1,string2
:
:
:
:
  #2 (permalink)  
Old 01-12-2007
maheshwin maheshwin is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 55
yea

use cut and paste command



cut -c1-10 filename > temp1
cut -c20-30 filename > temp2

paste temp1 temp2 > final

or
can use sed also
  #3 (permalink)  
Old 01-12-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed "s/\(.\{10\}\).\{9\}\(.\{10\}\).*/\1,\2/" file
  #4 (permalink)  
Old 01-12-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
awk '{ printf "%s,%s\n", substr($0, 1, 10), substr($0, 20, 30) }' filename
  #5 (permalink)  
Old 01-12-2007
maheshwin maheshwin is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 55
Quote:
Originally Posted by matrixmadhan
Code:
awk '{ printf "%s,%s\n", substr($0, 1, 10), substr($0, 20, 30) }' filename

the above script may give other output if we have spaces or blank lines, so better would be to use cut and paste command or the above sed script is again a correct one for your search
  #6 (permalink)  
Old 01-12-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
Originally Posted by maheshwin
the above script may give other output if we have spaces or blank lines, so better would be to use cut and paste command or the above sed script is again a correct one for your search
You are right, it will have a "," if there are blank lines

this would solve that,

Code:
awk ' { if( length($0) > 0 ) {printf "%s,%s\n", substr($0, 10, 20), substr($0, 20, 30) } }' filename
What if there are spaces? I dont get that really! Could you please explain?
  #7 (permalink)  
Old 01-12-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
If you have Python, here's an alternative:
Code:
o = open("newfile","a")
for line in open("input"):
    o.write( "%s , %s\n" % (line[0:10] , line[19:30] ) )
o.close()
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0