Replace Delimiters with Space


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace Delimiters with Space
# 1  
Old 09-03-2013
Replace Delimiters with Space

Hi All,
How to Replace the delimiter for a particular field. I have used awk to replace the field values based on the position, but I tried to remove/replace delimiters with space on particular positions.

I tried tr command with combination of awk not sure if this is the correct way, but I am not getting the required output. Is there any other way we can achieve this.

Code:
Sample text 1.

Data| 12345|U|76834

I want to replace the second and fourth pipe delimiter with space

Regards,
Mora.
Moderator's Comments:
Mod Comment Use CODE tags; not HTML tags to display sample input, output, and code samples.

Last edited by Don Cragun; 09-03-2013 at 10:28 PM.. Reason: Change HTML tags to CODE tags.
# 2  
Old 09-03-2013
Quote:
Originally Posted by mora
Hi All,
How to Replace the delimiter for a particular field. I have used awk to replace the field values based on the position, but I tried to remove/replace delimiters with space on particular positions.

I tried tr command with combination of awk not sure if this is the correct way, but I am not getting the required output. Is there any other way we can achieve this.

Code:
Sample text 1.

Data| 12345|U|76834

I want to replace the second and fourth pipe delimiter with space

Regards,
Mora.
Moderator's Comments:
Mod Comment Use CODE tags; not HTML tags to display sample input, output, and code samples.
I'm confused. You have 3 pipe delimiters and you want to change the 2nd and 4th???

Do all input lines have the same number of pipe delimiters?

Please show us sample input that matches your description of what you want to do AND show us sample output corresponding to that input.
# 3  
Old 09-03-2013
Hi ,

Sorry for the confusion. Below is my sample source data and expected output.

Source Data
HTML Code:
Data| 12345|U|76834|123
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |0
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |1
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |2
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |3
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |4
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |5

Expected Output

HTML Code:
Data| 12345U|76834123
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |0
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |1
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |2
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |3
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |4
Xyz|123| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |5
Regards,
Mora.
# 4  
Old 09-04-2013
Do you want to do this only in the header line of the file? If yes, then below command would work.

Code:
awk -F"|" '(NR==1){print $1 FS $2 $3 FS $4 $5;next} 1' filename

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

find & Replace text using two non-unique delimiters.

I can find and replace text when the delimiters are unique. What I cannot do is replace text using two NON-unique delimiters: Ex., "This html code <text blah >contains <garbage blah blah >. All tags must go,<text > but some must be replaced with <garbage blah blah > without erasing other... (5 Replies)
Discussion started by: bedtime
5 Replies

2. Shell Programming and Scripting

How to replace string between delimiters?

Hello, I would need to replace a delimiter in a flat file using.I would like to replace the semicolon (";") but only if it was contained in a string between quotes. For example: Original flat file example: abc;abc;"abc;abc";cd;"ef;ef";abc aa;bb;"aa";cc;"ddd;eee";ff Desired output:... (9 Replies)
Discussion started by: bartleby
9 Replies

3. Shell Programming and Scripting

Extract value between the delimiters and replace it with another value

Hi All, i have file name like below ABC_065224_123456_123456_your_130413_163005.txt ABC_065224_123456_MAIN_20130413_163005.txt ABC_065224_123456_123456_MAIN_130413_163005.txt ABC_065224_123456_123456_434567_MAIN_130413_163005.txt i need to find out the number of characters in the filed... (6 Replies)
Discussion started by: dssyadav
6 Replies

4. Shell Programming and Scripting

How to Replace string between delimiters?

Basically , i want to delete strings of a particular pattern from the flat file which is " | " pipe delimited. Below are the valid formats : 1) AAA (0) 111-111-111, AAA, BB 2) AAA (0) 111-111-1111;X, AAA, BB original flat file example : |ABC ABC XHAMK|AAA (0) 111-111-111, AAA,... (3 Replies)
Discussion started by: Ravi_007
3 Replies

5. UNIX for Dummies Questions & Answers

Remove two delimiters, space and double quotes

I would like to know how to replace a space delimiter with a ^_ (\037) delimiter and a double quote delimiter while maintaining the spaces inside the double quotes. The double quote delimiter is only used on text fields. I'd prefer a one-liner, but could handle a function or script that accepts... (4 Replies)
Discussion started by: SteveDWin
4 Replies

6. Shell Programming and Scripting

Processing data that contains space and quote delimiters

I need to write a Bash script to process a data file that is in this format: 1 A B C D E 2 F G "H H" I J As you can see, the data is delimited by a space, but there are also some fields that contain spaces and are surrounded by double-quotes. An example of that is "H H". I wrote... (7 Replies)
Discussion started by: RickS
7 Replies

7. Shell Programming and Scripting

Replace every other space

I'd like a sed command to replace every other space in my file. File: 0 1 0 3 0 2 0 5 Want: 01 03 02 05 Does anyone have any ideas? (9 Replies)
Discussion started by: peanuts48
9 Replies

8. Shell Programming and Scripting

Replace long space to become one space?

Hi, i have the log attached. Actually i want the long space just become 1 space left like this : Rgds, (12 Replies)
Discussion started by: justbow
12 Replies

9. Shell Programming and Scripting

Replace space

Hai masters, If a file contains content of 2000 lines, from which i need to remove the first n characters or first n spaces from each line of the file. If suppose to remove n characters or first n spaces from a single line means, just use the command nx. But from the above scenario,... (9 Replies)
Discussion started by: ecearund
9 Replies

10. Shell Programming and Scripting

replace space by _

Hi I need to know how I change the spaces by _ in folders and filder founded by find ex. find . -name "* *" -exec echo {} \; ./test space ./test space/new file.txt ./test space/new file ./test space/untitled folder ./test space/untitled folder/new fileruben ./Backup/backup/Image... (6 Replies)
Discussion started by: ruben.rodrigues
6 Replies
Login or Register to Ask a Question