How to replace the first and last character which is pipe symbol in all files within a folder?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace the first and last character which is pipe symbol in all files within a folder?
# 1  
Old 05-09-2018
How to replace the first and last character which is pipe symbol in all files within a folder?

with in my files i have the data like this, starting with a pipe and ending the line with a pipe.

all i want is to replace the first and last pipe , remove those
trying to use following sed command, but it is only showing on the screen the entire data of the file as if it removed, but when i open the file i again see the pipes at the beginning of each line and ending.
Code:
sed 's/^.\(.*\).$/\1/' /usr/local/bin/stage_process/tgfiles/20180301_svy_analysis.csv


Code:
|1273098536|A2|5|
|1273098536|A28|5|
|1273098536|A3|5|
|1273098536|A4|5|
|1273098536|A5|5|
|1273098536|B1|5|

that way they appear like this
Code:
1273098536|A2|5
1273098536|A28|5
1273098536|A3|5
1273098536|A4|5
1273098536|A5|5


Thank you very much for the helpful info, i am planning on using the commands within the shell files.

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 05-09-2018 at 04:21 PM.. Reason: Added CODE tags.
# 2  
Old 05-09-2018
By default sed prints to stdout (that you can redirect to a new file).
Some sed versions have a -i option to replace the input file by the output.
Code:
sed -i sedcode file_to_be_modified

# 3  
Old 05-09-2018
Code:
sed -i 's/^.\(.*\).$/\1/' usr/local/bin/stage_process/tgfiles/20180301_svy_analysis.csv

# 4  
Old 05-10-2018
As MadeInGermany says, some versions of sed have the -i option. If your version of sed does not, you will have to write something like:
Code:
file=/usr/local/bin/stage_process/tgfiles/20180301_svy_analysis.csv 
mv ${file} ${file}-bak
sed 's/^.\(.*\).$/\1/' ${file}-bak > ${file}

Don't try to write directly to the file without using an intermediate; the shell will open it for writing before it is opened for reading and the file contents will be lost.

Please include your Operating System and shell version in future queries; the solution can be tailored to your system.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 5  
Old 05-10-2018
Hello cplusplus1,

Could you please try following and let me know if this helps you.
Code:
awk '{gsub(/^\||\|$/,"")} 1' Input_file > temp_file  &&  mv  temp_file  Input_file

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove pipe(|) symbol in except the ones which are enclosed in double quotes

I have file with are delimited by pipe(|) symbol, I wanted those to be removed except the ones which are enclosed in double quotes. If your quote file is: |Life is |Beautiful"|"Indeed life |is beautiful too|"|"But unix is fun| is not"|" It should return: Life is Beautiful"|"Indeed life is... (9 Replies)
Discussion started by: Sathyapts
9 Replies

2. Shell Programming and Scripting

Broken pipe symbol replaced with <A6><A6>

hi, i am copying an xml file from windows to linux server using filezilla&winscp. xml file contains ¦¦ symbols, after copying xml file to server ¦¦ is replaced with <A6><A6>. tried with copying xml files from windows in ascii&binary but no luck. please suggest. thanks (1 Reply)
Discussion started by: Satyak
1 Replies

3. Shell Programming and Scripting

Remove/replace the very first character/symbol match

cat file.txt file 1123.x July 23:222 /cd/hh2/k39/ss2/f7d8d9d8e6r5t4s/dd2/e/s7a/s7a2afa5017d8b975-1.7-1395610245-b22e19bbc477b134 i wish to only extract out the 1.7 (anything within the first - -) i try to look for the sed command under match the first occurence of pattern but out of luck, my... (6 Replies)
Discussion started by: ctphua
6 Replies

4. Shell Programming and Scripting

Replace character in files of entire folder? sed? or what?

Hello, I do have several files in one folder each file contains measurement data. for each file I would like to replace the character "," by "." ? How can I do this and how can I do this for each file at once? E.G. data_1.dat, data_x.dat (original version) data_1out.dat, data_x_out.dat... (10 Replies)
Discussion started by: rollinator
10 Replies

5. Shell Programming and Scripting

Remove whitespace after pipe symbol but not inside words

I have a file that looks like this: 102| #2 X 1/4-INCH| 30188| EA| FTW| A| NOT SERIAL TRACKING| NOT LOT TRACKING| TRUE| #2 X 1/4-INCH 102| #2 X 1/4-INCH| 30188| EA| VPS| A| NOT SERIAL TRACKING| NOT LOT TRACKING| TRUE| #2 X 1/4-INCH 102| #6 X 1/2"| ... (2 Replies)
Discussion started by: djehresmann
2 Replies

6. Shell Programming and Scripting

Replace trailing whitespaces with pipe symbol using perl

I want to replace the whitespace with Pipe symbol to do a multiple pattern matching for the whole text "mysqld failed start" and same as for other text messages Below are the messages stored in a file seperate by whitespace mysqld failed start nfsd mount failed rpcbind failed to start for... (6 Replies)
Discussion started by: kar_333
6 Replies

7. UNIX for Dummies Questions & Answers

Removing ^M Character from all the files in a folder.

Hi Everyone, I am new and need your help. Can anyone please help me in removing the ^M character from all the file in a folder recursively. Please provide me the code. Response will be Really appreciated. Thanks in Advance (4 Replies)
Discussion started by: Manu_Learner
4 Replies

8. Shell Programming and Scripting

Problem while concating PIPE symbol with a file

Hi Gurus, I had a problem writing a pipe file. Previously i used this code to generate a tab seperated file ABCEF := ABCEF || 'to_char('|| abc_tab(col_num).col_name || ') chr(9) || '; Now i want the o/p as pipe seperated file.I changed the line as below ABCEF := ABCEF ||... (0 Replies)
Discussion started by: pssandeep
0 Replies

9. Shell Programming and Scripting

remove verticalbar or pipe symbol

hi guys i have 6000 rows column the text in the column has the symbol vertical bar |. i tried some of the commands to remove it but none of the commands are reconzng this symbol. would u plz help to remove this symbol from the text with any kind of unix command u r help would be appreciated ... (9 Replies)
Discussion started by: bogu0001
9 Replies

10. Shell Programming and Scripting

How To replace Control-M in all files in a folder

hi all, I copied set of files from a linux machine to an aix machine but in binary mode copy , ASCII mode copy both leed to control M charecters in most of the files. Any shell script/C script to remove control M charecters in all files in a given directory. Pls reply if you are aware... (10 Replies)
Discussion started by: padpa
10 Replies
Login or Register to Ask a Question