Very basic problem for a basic user....counting pipes and appending to the end of line.

Very basic problem for a basic user....counting pipes and appending to the end of line.

Post by Sean Mora » Fri, 26 Apr 2002 11:57:19



I am trying to work through a file line by line...each line needs to have a
total of 4 "|" (pipes) that divide columns for a database load file.
Some have four already, some don't.
What is the best way to check each line and just append pipes to the end of
the lines that need them?

For example:

1234| Blah blah blah |
1235| Blah blah blah |ljfldkjf|

would become

1234| Blah blah blah | ||
1234| Blah blah blah |ljfldkjf||

Thanks in advance...

 
 
 

Very basic problem for a basic user....counting pipes and appending to the end of line.

Post by John W. Krah » Fri, 26 Apr 2002 12:19:17



> I am trying to work through a file line by line...each line needs to have a
> total of 4 "|" (pipes) that divide columns for a database load file.
> Some have four already, some don't.
> What is the best way to check each line and just append pipes to the end of
> the lines that need them?

> For example:

> 1234| Blah blah blah |
> 1235| Blah blah blah |ljfldkjf|

> would become

> 1234| Blah blah blah | ||
> 1234| Blah blah blah |ljfldkjf||

> Thanks in advance...

perl -pli~ -e'$_.="|"while y/|//<4'  yourfile

John
--
use Perl;
program
fulfillment

 
 
 

Very basic problem for a basic user....counting pipes and appending to the end of line.

Post by Chris F.A. Johnso » Fri, 26 Apr 2002 13:12:12



> I am trying to work through a file line by line...each line needs to have a
> total of 4 "|" (pipes) that divide columns for a database load file.
> Some have four already, some don't.
> What is the best way to check each line and just append pipes to the end of
> the lines that need them?

> For example:

> 1234| Blah blah blah |
> 1235| Blah blah blah |ljfldkjf|

> would become

> 1234| Blah blah blah | ||
> 1234| Blah blah blah |ljfldkjf||

        while read line
        do
            while :
            do
               case $line in
                  *\|*\|*\|*\|*) break ;;
                  *\|*\|*\|*) line="$line |" ;;
                  *\|*\|*) line="$line ||" ;;
                  *\|*) line="$line |||" ;;
                  *) line="$line ||||" ;;
               esac
            done
            echo "$line"
        done < file

--

        =================================================================


 
 
 

Very basic problem for a basic user....counting pipes and appending to the end of line.

Post by Tapani Tarvaine » Fri, 26 Apr 2002 14:25:18



> I am trying to work through a file line by line...each line needs to have a
> total of 4 "|" (pipes) that divide columns for a database load file.
> Some have four already, some don't.
> What is the best way to check each line and just append pipes to the end of
> the lines that need them?

> For example:

> 1234| Blah blah blah |
> 1235| Blah blah blah |ljfldkjf|

> would become

> 1234| Blah blah blah | ||

                        ^

where's that space supposed to come from?

Quote:> 1234| Blah blah blah |ljfldkjf||

Assuming the extra space above was a typo, this should do:

awk -F\| '{$5=$5; print}' OFS=\| file

--
Tapani Tarvainen

 
 
 

Very basic problem for a basic user....counting pipes and appending to the end of line.

Post by Glen Burs » Fri, 26 Apr 2002 17:15:55


There might be a more elegant way, but you could do this with awk e.g.

# echo 'aa a|a|a' |  awk -F\| '{if (NF == 3) print $0 "|"}'
aa a|a|a|

You could then to the whole file like -:

# cat yourfile | awk -F\| '{if (NF == 3) print $0 "|"}'
...
...

Glen.


> I am trying to work through a file line by line...each line needs to have a
> total of 4 "|" (pipes) that divide columns for a database load file.
> Some have four already, some don't.
> What is the best way to check each line and just append pipes to the end of
> the lines that need them?

> For example:

> 1234| Blah blah blah |
> 1235| Blah blah blah |ljfldkjf|

> would become

> 1234| Blah blah blah | ||
> 1234| Blah blah blah |ljfldkjf||

> Thanks in advance...

 
 
 

Very basic problem for a basic user....counting pipes and appending to the end of line.

Post by laura fairhe » Fri, 26 Apr 2002 18:08:11



>I am trying to work through a file line by line...each line needs to have a
>total of 4 "|" (pipes) that divide columns for a database load file.
>Some have four already, some don't.
>What is the best way to check each line and just append pipes to the end of
>the lines that need them?

>For example:

>1234| Blah blah blah |
>1235| Blah blah blah |ljfldkjf|

>would become

>1234| Blah blah blah | ||
>1234| Blah blah blah |ljfldkjf||

>Thanks in advance...

Many ways to do this with 'sed', you could add one for each one that
isn't there;

sed '/|.*|.*|.*|/!s/$/|/
     /|.*|.*|.*|/!s/$/|/
     /|.*|.*|.*|/!s/$/|/
     /|.*|.*|.*|/!s/$/|/
     ' filename

Or you could add too many and take them away;

sed 's/$/||||/
     s/\([^|]*|[^|]*|[^|]*|[^|]*|[^|]*\).*/\1/
     ' filename

byefrom

--

                # if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492

 
 
 

Very basic problem for a basic user....counting pipes and appending to the end of line.

Post by Al Shark » Fri, 26 Apr 2002 21:28:25



} I am trying to work through a file line by line...each line needs to have a
} total of 4 "|" (pipes) that divide columns for a database load file.
} Some have four already, some don't.
} What is the best way to check each line and just append pipes to the end of
} the lines that need them?
}
} For example:
}
} 1234| Blah blah blah |
} 1235| Blah blah blah |ljfldkjf|
}
} would become
}
} 1234| Blah blah blah | ||
} 1234| Blah blah blah |ljfldkjf||

awk 'BEGIN {FS = "|"; OFS = "|"};{print $1,$2,$3,$4,$5}' yourfile > newfile

Assuming that you have only 5 or less fields on any given line.

 
 
 

Very basic problem for a basic user....counting pipes and appending to the end of line.

Post by Peter J. Ackl » Fri, 26 Apr 2002 22:39:18



> awk 'BEGIN {FS = "|"; OFS = "|"};{print $1,$2,$3,$4,$5}' yourfile > newfile

Just an alternative...

   perl -wpe 's/$/ "|" x (4 - tr-|-|-) /e' yourfile > newfile

Peter

--
Where do bit streams end?  In bit rivers?

 
 
 

Very basic problem for a basic user....counting pipes and appending to the end of line.

Post by Sean Mora » Fri, 26 Apr 2002 22:50:11


THANKS EVERYONE!

I really appreciate it.

Sean

 
 
 

1. Basic Authentication with Groups and Large User Count

I read the article from the hotwired team:
http://www.webmonkey.com/webmonkey/html/97/08/index2a.html

about Basic authentication - not because I don't know how it works,
just interest. It says
        "One important thing to note here is that the group file
           doesn't work very well for large lists. If you've got a
           lot of users, you might want to consider some
           database options."

What size is "large"?
What are these database options?

I'm expecting tens of thousands to use my site (being forced to
register and using a group file). I'm using IServer (HTTPD), should I
expect problems ?

TIA

Brian

http://www.dkonline.com/twtw2/
Username: TRIAL Password: TRUNK

2. what's disk's label

3. Basic and Basic->C converter wanted

4. How to mount on a remote host with solaris 2.4

5. No ROM BASIC Use Toshiba Basic

6. HELP! DHCP & Windows!!!

7. basic, very basic

8. sh -nv hangs on while loops

9. Basic basic troff question??!

10. The most basic of basics..

11. Basic Compiler/Basic-to-C Compiler

12. Basic, Basic SCSI Question

13. very basic basic newbie question