Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joining two lines 4

Status
Not open for further replies.

ehope1

Technical User
Feb 4, 2005
16
0
0
US
I'm working with a korn shell script and have data that looks like:

SPE-2 Bread Basket
Quantity: 2
Each: $12.00

I want the 1st line to read:
Quantity: 2 SPE-2 Bread Basket

I know how I can join or append the 1st two lines together
(sed -e '/^Quantity:/N;s/\n/ /g') but I don't know what to do if I want the second line to precede the first on the same line.

Any help would be appreciated!

 
Can you provide a larger sample of the input file. The solution would probably be based upon the repeating pattern in that file. Is it just the first two lines you care about?
 
This would simply manipulate those first two lines, and leave the remainder of the file "as is":

Code:
$ cat doit
first=`head -1 file1`
second=`head -2 file1|tail -1`
echo $second  $first >newfile
tail +3l file1 >>newfile

 
Here's a larger sample of the file:
Date: 12/29/05 12:00:54 PST
From: Harry Truman <htruman@aol.com>
Subject: 60622 - Secure Online Credit Card Transaction
_______________________________________
Customer/Shopper ID: 60622
Date and Time: 12/29/05 12:00:54 PST
_______________________________________
Bread Basket
Quantity: 2
Details: Red & White
Each: $ 21.00
Item Total: $ 42.00
_______________________________________
Bookends
Quantity: 1
Details: Single Bookend
Black
Each: $ 18.95
Item Total: $ 18.95
_______________________________________

Sub Total: $ 60.95
Shipping Total: (Continental USA) $ 6.95
Grand Total: $ 67.90

I want to be able to put the Quantity line up on product line.


 
I think you'll find what you need in the "awk" forum. Check this thread:

thread271-920361

There are some very smart folks there, and they can probably help you.
 
How about something like:
[tt]
perl -e "\
$s.=$_ while(<>);\
$s=~s/\n(.+?)\n(Quantity: \d+?)/\n$2 $1/g;\
print $s;"\
<infile >outfile
[/tt]
 
Sorry, that second question mark should be removed from the regex.
 
Thanks to both motoslide and TonyGroves! I'll try both options and see what happens.
 
Here's a Korn shell solution...
Code:
#!/bin/ksh

FILE=inputfile.dat
LAST=""

unset IFS

while read LINE
do
    if [[ ${LINE} = @(Quantity:*) ]]
    then
        print "${LINE} ${LAST}"
    else
        [[ ${LAST} != @(Quantity:*) ]] && print "${LAST}"
    fi

    LAST="${LINE}"

done < ${FILE} | tail +2

tail -1 ${FILE}
 
Thanks. Korn shell is what I want to use... even though I'm kind of new at it...I understood what was going on in your code until the last two lines. What does the following do?

< ${FILE} | tail +2

tail -1 ${FILE}
 
[tt]< ${FILE}[/tt]

Uses the filename in the FILE variable as input to the while read loop.

[tt]| tail + 2[/tt]

Displays the output of the while read loop starting from the second line (i.e. skips the first).

[tt]tail -1 ${FILE}[/tt]

This displays the last line of the file (which wasn't printed by the while read loop.

Not sure why you didn't use a print $LAST here Sam?

Annihilannic.
 
The loop was adding a blank line at the beginning. The "[tt]| tail +2[/tt]" just removes the blank first line since it wasn't part of the original file.

Change the "[tt]tail -1 ${FILE}[/tt]" to "[tt]print ${LAST}[/tt]" and give it a try. The last line doesn't print. With my Korn shell, when the file is fed to the loop with the "[tt]< ${FILE}[/tt]", the code block in the loop appears to become a seperate process, so the value of the variable [tt]LAST[/tt] doesn't make it to the [tt]print[/tt] statement. My first shot at this did have a "[tt]print ${LAST}[/tt]", but it was missing that last line. Weird.
 
Thanks to both of you. I appreciated the explanation of the "tail" piece. I got this to work so I'm a happy camper. Thanks to all who replied!
 
Hi Sam,

Ah... did you do it on Linux? I've only noticed that happening on Linux.

Annihilannic.
 
Ah... okay, my mistake; in this case it doesn't seem to be a variable scope thing; it looks like a while read loop will set the variable to an empty value when it reads EOF. You can see that the variable is still set in the environment, but has no contents.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top