Fold multiple lines with sed or awk

Fold multiple lines with sed or awk

Post by Ningli » Tue, 07 Aug 2001 02:34:26



Hi,

    I have a file with the following format:

    Company        Date        Plant  Code    
    companyA    04/18/01    114243433    
                                         04924          99999
                                                                Status:  
unknown
    companyB   11/18/99      E000ADP4855
                                                   N/A
                                                                Status: know
    companyC   12/23/00      123124344
                                       Contact Sales: wrong number            

    ................

    Each company has information spreading over multiple lines and each company
may have different lines of information. ( 1 to 4 lines). I know first column
(companyA ...) and second column (Date) always start at certain position. Other
lines may not.
    My question is: how can I "fold" multiple lines to each company so that the
final output will look like the following:
    companyA   04/18/01  114243433   04924111412     99999   Status:   unknown
   companyB   11/18/99   E000ADP4855                     N/A     Status: known
    companyC   12/23/00   123124344   Contact Sales: wrong number    

    I try to use awk array but couldn't get the result.

    Thanks in advance for your advice.

    Nick

 
 
 

Fold multiple lines with sed or awk

Post by B. Caligar » Tue, 07 Aug 2001 08:24:15



Quote:> Hi,

>     I have a file with the following format:

>     Company        Date        Plant  Code
>     companyA    04/18/01    114243433
>                                          04924          99999
>                                                                 Status:
> unknown
>     companyB   11/18/99      E000ADP4855
>                                                    N/A
>                                                                 Status:
know
>     companyC   12/23/00      123124344
>                                        Contact Sales: wrong number

>     ................

<snip>

Quote:>     My question is: how can I "fold" multiple lines to each company so
that the
> final output will look like the following:
>     companyA   04/18/01  114243433   04924111412     99999   Status:
unknown
>    companyB   11/18/99   E000ADP4855                     N/A     Status:
known
>     companyC   12/23/00   123124344   Contact Sales: wrong number

>     I try to use awk array but couldn't get the result.

<snip>

try using s with the -n parameter,

e.g.  sed -n -f scriptname filename

There's a 'very rough' regex match on the date \d{2}\/\d{2}\/\d{2}.  The
sample was not very clear, but if you said that the company name and ate are
easy to match, zeroing on the record start should be quite easy.

B

---------------------
$b output
/[012345679][0123456789]\/[012345679][0123456789]\/[012345679][0123456789]/b
output
H
b
:output
x
s/\n/ /g
s/  */ /g
p
---------------------

 
 
 

Fold multiple lines with sed or awk

Post by Chris F.A. Johnso » Tue, 07 Aug 2001 08:41:04



Quote:> Hi,

>     I have a file with the following format:

>     Company        Date        Plant  Code
>     companyA    04/18/01    114243433
>                                          04924          99999
>                                                                 Status:
> unknown
>     companyB   11/18/99      E000ADP4855
>                                                    N/A
>                                                                 Status: know
>     companyC   12/23/00      123124344
>                                        Contact Sales: wrong number

>     ................

>     Each company has information spreading over multiple lines and each company
> may have different lines of information. ( 1 to 4 lines). I know first column
> (companyA ...) and second column (Date) always start at certain position. Other
> lines may not.
>     My question is: how can I "fold" multiple lines to each company so that the
> final output will look like the following:
>     companyA   04/18/01  114243433   04924111412     99999   Status:   unknown
>    companyB   11/18/99   E000ADP4855                     N/A     Status: known
>     companyC   12/23/00   123124344   Contact Sales: wrong number

>     I try to use awk array but couldn't get the result.

>     Thanks in advance for your advice.

>     Nick

awk '    BEGIN {getline} ## get rid of header

## regular expression matches the beginning of a record;
## adjust to taste
/............[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/ && NR > 2 {
    ## print as newline for each new record after the first
    print ""
    }

    {
    printf "%s ", $0 ## print without a newline
    }

    END {print ""}
' file

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

1. Fold multiple lines with sed or awk

Hi,

    I have a file with the following format:

    Company        Date        Plant  Code    
    companyA    04/18/01    114243433    
                                         04924          99999
                                                                Status:  
unknown
    companyB   11/18/99      E000ADP4855
                                                   N/A
                                                                Status: know
    companyC   12/23/00      123124344
                                       Contact Sales: wrong number            

    ................

    Each company has information spreading over multiple lines and each company
may have different lines of information. ( 1 to 4 lines). I know first column
(companyA ...) and second column (Date) always start at certain position. Other
lines may not.
    My question is: how can I "fold" multiple lines to each company so that the
final output will look like the following:
    companyA   04/18/01  114243433   04924111412     99999   Status:   unknown
   companyB   11/18/99   E000ADP4855                     N/A     Status: known
    companyC   12/23/00   123124344   Contact Sales: wrong number    

    I try to use awk array but couldn't get the result.

    Thanks in advance for your advice.

    Nick

2. master boot record

3. Fold multiple lines using sed or awk

4. kbdrate

5. Extracting multiple lines OR deleting multiple lines from a file using AWK

6. Need Linux

7. Grep/awk/sed for multiple/groups of lines in a file???

8. anyone take the LPI Linux exam?

9. Pattern Matching Multiple Lines with SED and AWK

10. Printing multiple lines with sed/awk

11. find and replace multiple lines using awk or sed