deleting pipes in a particular filed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting deleting pipes in a particular filed
# 1  
Old 10-15-2007
deleting pipes in a particular filed

i have a file with some records seperated by pipe. I am getting unwanted "|" in the 7th field and i want to remove any pipes in the
7th field only. Can somebody help out? Here is the sample record.


460625192|432559595|MANU MC|2013/12/01||2007/09/24|-R@a{[^U|O?6-\u??^L ?|100|5|Y|||1|9100380020070924|2007/10/10 20:40:21|9999/12/31 01:00:00|Y|54113|OPUS|PLASTIC|X13253U89MCKA|1|2007/10/10 20:40:21
# 2  
Old 10-15-2007
Hi.

How would a program determine that there is an embedded symbol, as opposed to the normal field separator symbol? Is there some unique context in which the embedded symbol appears?

Off-hand, I'd say that this would need to be taken care of when the file is created ... cheers, drl
# 3  
Old 10-16-2007
Hi.

Here is one kind of solution. It depends on accepting that the first line has the correct number of fields, and that all errors ( extra separators ) are confined to a single field. In this example, that's field 3. If an anomaly occurs, then the extra separator is removed.
Code:
#!/usr/bin/env sh

# @(#) a1       Demonstrate embedded field separator correction.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "(Versions used in this script displayed with local utility "version")"
version bash awk

echo

# Create data file.

cat >data1 <<'EOF'
now|is|the|time
for|all|go|od|men
to|come|to|the
aid|of|their|country
EOF

# This assumes that if an embedded separator appears, it will be
# in field 3, and so the separator number 3 will be replaced with
# the null string.  This is essentially a trial-and-error
# solution.

echo " Data file:"
cat data1

echo
echo " Results of correction:"
awk '
BEGIN   { FS = "|" }
NR == 1 { f = NF }
        {
                if ( NF != f ) {
                        print " BANG! found discrepancy at line", NR # remove as desired
                        $0 = gensub("[|]","",3,$0)
                }
                print $0
        }
' data1

exit 0

Producing:
Code:
% ./a1

(Versions used in this script displayed with local utility version)
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU Awk 3.1.4

 Data file:
now|is|the|time
for|all|go|od|men
to|come|to|the
aid|of|their|country

 Results of correction:
now|is|the|time
 BANG! found discrepancy at line 2
for|all|good|men
to|come|to|the
aid|of|their|country

See man pages for details, modify as necessary ... cheers, drl
# 4  
Old 10-19-2007
TRY THIS OUT !!!!!!!!!!!!!!!!!!!!!!!!!!

cut -d'|' -f7,8 filename | tr -d '|'
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk multiple filed separators

There is an usual ifconfig output vlan30 Link encap:Ethernet HWaddr inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: 2407:4c00:0:1:aaff::1/64 Scope:Global inet6 addr: fe80::224:e8ff:fe6b:cc4f/64 Scope:Link UP BROADCAST... (1 Reply)
Discussion started by: urello
1 Replies

2. Shell Programming and Scripting

Awk with mutliple filed separators

I try to trim a FTP log using awk, but the escape sequences does not work the way I want it. Input data Wed 3Oct10 21:48:00 - (002117) Sent file d:\ftp\home\tools\htmake.rar successfully (48.2 kB/sec - 40997 Bytes) Wed 3Oct10 22:25:46 - (002118) Sent file d:\ftp\files\main\oct\camera1... (4 Replies)
Discussion started by: Jotne
4 Replies

3. Shell Programming and Scripting

how to add empty filed to record

hi i have record looks like below 1,US I want to add empty field to the record as below 1, , , ,US how i can do it using awk ? i tried with awk its not working awk '{ print $1", ,"$2 }' filename > file 1 (2 Replies)
Discussion started by: raghavendra.cse
2 Replies

4. UNIX for Dummies Questions & Answers

sed to edit nth filed

Hi all, I want to edit nth filed of a comma delimited line with some value. Can I use sed command to do this. Pls suggest me the command here. Thanks, Poova. (2 Replies)
Discussion started by: poova
2 Replies

5. Linux

The LimitRequestBody filed in openSUSE 10.2

Because some program i have installed can not upload files bigger than some size as printing, the base got a packet bigger than 'max_allowed_packet' bytes what that i need to do as written in the documentation of the program increase the value of a fileld called LimitRequestBody told that is in... (5 Replies)
Discussion started by: tal
5 Replies

6. UNIX for Dummies Questions & Answers

The LimitRequestBody filed in openSUSE 10.2

Because some program i have installed can not upload files bigger than some size as printing, the base got a packet bigger than 'max_allowed_packet' bytes what that i need to do as written in the documentation of the program increase the value of a fileld called LimitRequestBody told that is in... (3 Replies)
Discussion started by: tal
3 Replies

7. Shell Programming and Scripting

how do i ignore rows with first filed NULL

I have a file to load in the table, and am using SQLLDR CONTROL FILE ----------------------- LOAD DATA INFILE 'sample.txt' APPEND INTO TABLE TEMP_LOAD FIELDS TERMINATED BY '|' TRAILING NULLCOLS (FIELD1,FIELD2,FIELD3) Now i have about 10,000 lines in the file FIELD1 in the table is... (1 Reply)
Discussion started by: prash184u
1 Replies

8. Shell Programming and Scripting

Filed substitution with awk

guys, I'm trying to 9k lines of the following: aaa aaa 1 1 1 to aaa aaa 1 01 1 Im pretty ignorant when it comes to subtituting fields using awk any help ? Tony (1 Reply)
Discussion started by: tony3101
1 Replies

9. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question