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
sed query gopsman Shell Programming and Scripting 4 02-03-2009 02:41 AM
& in SQL query Lindarella Shell Programming and Scripting 3 10-09-2006 04:43 PM
Query In awk raguramtgr UNIX for Dummies Questions & Answers 1 08-27-2004 11:00 AM
RPM Query silvaman UNIX for Advanced & Expert Users 1 08-15-2003 07:56 AM

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

Join Date: Nov 2006
Posts: 17
sed query

I have a series of folders /temp/a /temp/b /temp/c

In folders a, b, and c, I have files
a1.txt……….a20.txt
b1.txt……….b40.txt &
c1.txt……….c60.txt

Each file has the same data format :-

Line 1 AAAAA aaaa
Line 2 BBB bbbbbb
Line 3 CCCC cccccc
Etc etc

I need to write a sed script which will go through all the files and reformat the data thus :-

Line 1 AAAAA aaaa
Line 2 BBB
Line 3 X bbbbbb
Line 4 CCCC cccccc
Etc etc

So I need to insert a CR/LF in line 2 position 4,
create a new Line 3 with a constant text (X) and the value of what was to the right of the previous Line 2 (bbbbbb)
and move the remaining lines one line down.

Can anyone help please ?
I don't know sed very well, and awk not at all.
  #2 (permalink)  
Old 12-07-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Does this work?
Code:
awk '{if(NR==2) print $1,"\nX",$2; else print}' filename > filename.new
mv filename.new filename
Run this through a loop on each file.

Note that I have assumed that the text in your files is just as you are showing it (exactly the same number and location of space characters to be specific). Even if it isn't you just need to change the awk a bit.
  #3 (permalink)  
Old 12-08-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
SED solution

Code:
cd /a
for file in *
do
    sed "2 s/\(...\).\(.*\)/\1\\
    X \2/" $file > tmp
    mv tmp $file
done
  #4 (permalink)  
Old 12-08-2006
grinder182533 grinder182533 is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 17
sed solution

Thanks Anbu23
When I posted yesterday, I simplified my problem, thinking that I could follow your logic, and apply it to my actual problem.
In fact, it is line 5 position 32 before which the CR/LF is to be inserted.
The new line 6 will contain fixed text from position 1 to 31 inclusive, (X)
and position 32 to 34 from line 5 will be dropped down to the same positions on line 6.
I tried
sed "5 s/\(...\).\(.*\)/\1\\
X \2/" $file > tmp
but this still spilt after 3 characters.

I tried
sed "5 s/\(.............................\).\(.*\)/\1\\
X \2/" $file > tmp
but got a syntax error.
Hope you can further help.
Thanks
  #5 (permalink)  
Old 12-08-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Previously you told that after first few chars in some line has to be moved to next line with X.But now you are telling that you need initial char and X and remaining char in new line.I am not able to get you.
Can you give me example?
  #6 (permalink)  
Old 12-08-2006
grinder182533 grinder182533 is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 17
sed example

Hi Anbu23

input

Ref: 12345678^M
Value Date: 28-MAY-2006^M
Debit A/c: 54321235 - abcdef^M
Customer : 100210547 - abcdef^M
Debit Amount: 9999999999.00 USD^M
Description: wire to Wxxxxxx^M
^M
Exchange Rate: 1.00000^M
etc etc

should turn into

Ref: 12345678^M
Value Date: 28-MAY-2006^M
Debit A/c: 54321235 - abcdef^M
Customer : 100210547 - abcdef^M
Debit Amount: 9999999999.00^M
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx USD^M
Description: wire to Wxxxxxx^M
^M
Exchange Rate: 1.00000^M
etc etc

cutting & pasting has offset the display above,
but my earlier line 5 position 31 is accurate.
Please also note that there are lots of records in each file,
and all have to be reformatted.
Many thanks again
I do appreciate your support.
  #7 (permalink)  
Old 12-08-2006
cskumar cskumar is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 67
try this one

awk '{if (match($0,"USD")){print $1 $2 $3,"\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",$4} print $0}' file > newfile

I hope USD will come only in Debit Amount: field.

Regards,
cskumar
Sponsored Links
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:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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