The UNIX and Linux Forums  


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
aside from paste kenshinhimura Shell Programming and Scripting 3 03-11-2008 01:16 AM
paste several files tonet Shell Programming and Scripting 1 10-05-2007 11:08 AM
cut & paste t_harsha18 Shell Programming and Scripting 3 10-02-2005 04:16 AM
paste command mariner UNIX for Advanced & Expert Users 5 03-04-2005 02:42 AM
awk or sed or paste leprichaun UNIX for Dummies Questions & Answers 3 11-16-2003 07:58 PM

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

Join Date: Dec 2005
Posts: 25
Cut Paste and Insert Help

Hello

I have a very large file where say each line is made up of 80 characters.
I want to cut the characters from 20-30 and 50-60 from each line and then insert a delimiter between them (# or | etc).
eg
input file
000000000131.12.20990000590425246363375670011200140406
000000000131.12.20990000599116259067695570011200140306
000000000131.12.20990000954757586517205670011200140206
000000000131.12.20990001100136286779157070040749470106
000000000131.12.20990001100331729172935570040774630106
000000000131.12.20990001100417542814864770040736610106
000000000131.12.20990001100565052159608570040771070106

output file
209900005#700112001404
209900005#700112001403
209900009#700112001402
209900011#700407494701
209900011#700407746301
209900011#700407366101
209900011#700407710701



I can cut the relevant characters and put them in 2 files and then using cut paste option make a new file with the desired results. However this will take a long time as the file is huge. Is there a simpler way.
Please help.

Thanks in advance

Regards
Pradeep
  #2 (permalink)  
Old 12-07-2005
rajus19 rajus19 is offline
Registered User
  
 

Join Date: Feb 2005
Location: Bangalore
Posts: 36
first cut the relavent columns and then store in a temp files, then use the paste command to insert the delimiter then at last remove the temp files.

cut -c 20-30 data > t1 ; cut -c 50-54 data > t2 ; paste -d "#" t? ; rm t?
  #3 (permalink)  
Old 12-07-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
If it is always 20-30 and 50-60, you can use the bash builtin to do what cut does.

Something like


Code:
LHS=${line:20:10}
RHS=$(line:50:10}
OUTPUT="${LHS}#${RHS}"

See this post

See man sh for further info.
  #4 (permalink)  
Old 12-07-2005
PradeepRed PradeepRed is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 25
I request you to pls elaborate on the code given. If my data is present in a file named a1, then how do i use your code to get a file a2 with the desired output. I could not figure out how to use the variables LHS and RHS to extract data from a line in a file.

Regards
  #5 (permalink)  
Old 12-07-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798

Code:
sh-2.05b$ cat pradeep.sh 
#! /bin/sh

while read line
do
LHS=${line:16:9}
RHS=${line:40:12}
echo "$LHS#$RHS" >> pradeep
done < pradeep.txt
sh-2.05b$ cat pradeep.txt 
000000000131.12.20990000590425246363375670011200140406
000000000131.12.20990000599116259067695570011200140306
000000000131.12.20990000954757586517205670011200140206
000000000131.12.20990001100136286779157070040749470106
000000000131.12.20990001100331729172935570040774630106
000000000131.12.20990001100417542814864770040736610106
000000000131.12.20990001100565052159608570040771070106
sh-2.05b$ ./pradeep.sh 
sh-2.05b$ cat pradeep
209900005#700112001404
209900005#700112001403
209900009#700112001402
209900011#700407494701
209900011#700407746301
209900011#700407366101
209900011#700407710701
sh-2.05b$

  #6 (permalink)  
Old 12-09-2005
PradeepRed PradeepRed is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 25
Thanks a lot Vino !!!!!!!!
Worked like a charm
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 02:22 AM.


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