Remove pipes in file using SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove pipes in file using SED
# 1  
Old 10-17-2007
Remove pipes in file using SED

Hello,

I am writing a script to remove multiples pipes (|) from a file. It needs to look like this data|data|data
instead of data|||data||data||||data

I have found the correct sed command to do this, but I need to do it in a loop and a for loop has not worked for me. It needs to be done on multiples files and then write to a new file. I'm running Solaris 8. I believe I need to use a while loop, but am unsure how do do this.
Here is what I have thus far:

#!/bin/sh
file="/file/directory/bin/temp/*.fixed"

#Removes extra spaces and Pipes in the file

for filename in *.fixed
do
sed 's/ * //g;s/||/|/g;s/|||/|/g' $file > newFile.fixed
done
# 2  
Old 10-17-2007
Code:
for f in *.fixed;do sed 's/||*/|/g' "$f">new_"$f";done

or just:

Code:
awk '{gsub(/\|\|*/,"|");print>("new_"FILENAME)}' *.fixed

P.S. Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 10-17-2007
Quote:
Originally Posted by TL56
sed 's/ * //g;s/||/|/g;s/|||/|/g' $file > newFile.fixed
more or less. The following will catch not only double and triple occurences of pipe-chars, but any number exceeding 1 (only the sed-code):

s/|[|]*/|/g

This replaces 1 or more occcurence of a pipe-char with exactly 1. The "[...]" means "any of the characters inside the brackets" and "*" means "zero or more of the last-mentioned expression", which is the brackets in this case. "[abc]*" would match any number of the characters a, b and c, i.e. "cbaabbc" or "a", but not "d".

About the multiple files issue, yes, you will have to use a loop. You can use the for-loop you have mentioned, but there will be a problem it the directory contains a very big number of files. "*" expands to a list of all filenames and this could get bigger than the maximum command line length in UNIX (for POSIX systems this is IIRC 4k, see /usr/include/sys/limits.h). You can overcome this by either using a temporary file:

Code:
ls -1 /files/to/change > /tmp/file
cat /tmp/file | while read file ; do
   sed ..... ${file} > ${file}.changed
   mv ${file}.changed ${file}
done

or you could use a pipe:

Code:
ls -1 /files/to/change | while read file ; do
   sed ..... ${file} > ${file}.changed
   mv ${file}.changed ${file}
done

bakunin
# 4  
Old 10-17-2007
Thank you bakunin and radoulov.

bakunin
The code s/|[|]*/|/g worked. Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove the first match only in a file using sed

I'm trying to remove the first match only of 2Z694 from an xml file and replace with a blank File Example: </Phoenix_Response_Data> <Bundle_Name_Primary>2Z694</Bundle_Name_Primary> <Bundle_Name>2Z694</Bundle_Name> </Phoenix_Response_Data> tried using: sed -e 's/'2Z694'/''/1' but this... (15 Replies)
Discussion started by: cillmor
15 Replies

2. UNIX for Advanced & Expert Users

Trying to use sed to remove last FF from file

I have a script that I am trying to apply on files that have form feeds between pages but I am trying to replace the last form feed, with carriage return so that when I convert it to a PDF file it won't generate a blank page. My script looks like this sed '$ s/\^L/\^M/' invoice.txt... (12 Replies)
Discussion started by: ziggy6
12 Replies

3. Shell Programming and Scripting

Remove pipes from end of file

Hello all, I have a file like this AA|| BB|| CC|| I want to remove the pipelines (||) from the last occurrance of them in file. that means after removal my file should be like this AA|| BB|| CC how can we achieve this? (2 Replies)
Discussion started by: nnani
2 Replies

4. Shell Programming and Scripting

remove particular line from a file using sed

Hi i need to remove all the lines staring with 'printf("\n' from a file, example : the file tmp.txt contains printf("\n "); printf("\n good"); printf("\n "); printf("\n "); printf(""); printf( m_sprintf(for printf("\n "); i have tried with following commands but... (5 Replies)
Discussion started by: mprakasheee
5 Replies

5. Shell Programming and Scripting

sed to remove braces from a file

i need to search for user belonging to group 'macusr' and the extract the user name . i am able to write a oneliner for this using awk + sed + tr i am using tr to chop off '()' from the output. but i want to use it in sed itself . can someone please help me with that file contents ... (7 Replies)
Discussion started by: chidori
7 Replies

6. Shell Programming and Scripting

sed -e remove line from file

Hi Trying to remove line from file log_January_1_array and code below doesn't work. $(sed -e '/"$n"/d' <log_January_1_array >log_January_1_array_1) sed doesn't know what is in $n variable and nth happens. Please advice how to make sed running this. thx (2 Replies)
Discussion started by: presul
2 Replies

7. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies

8. HP-UX

remove named pipes

Hi, Please help me on this. I am creating a named pipe in a kshell script. I am using mkfifo pipe_name command to create the pipe. I want to remove the named pipe after my work is completed. How can i do that. (8 Replies)
Discussion started by: chintapalli001
8 Replies

9. Shell Programming and Scripting

how to remove ^@ from a file using sed...or anything

i tried the following:- sed -e file 's/^@//g' > temp also tried sed -e file 's///g' > temp nothing happened....can someone please tell me wht is wrong??? also someinformation abt the character "^@"(it is ONLY ONE character and NOT TWO characters) thanx in advance.. (13 Replies)
Discussion started by: sayonm
13 Replies

10. 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