How to change a specific character in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change a specific character in a file
# 1  
Old 02-21-2008
How to change a specific character in a file

Hi,

I have a data file with following structure:

a|b|c|d|3|f1|f2|f3
a|b|c|d|5|f1|f2|f3|f4|f5

I want to change this data to:

a|b|c|d|3|f1;f2;f3
a|b|c|d|5|f1;f2;f3;f4;f5

Data in column 5 tells the number of following fields. All fields delimiter after the 5th column needs to be changed to ";"

Please help how can this be done in any scripting language sed, awk or perl?

Regards

Sandeep
# 2  
Old 02-21-2008
File
==
a|b|c|d|3|f1|f2|f3
a|b|c|d|5|f1|f2|f3|f4|f5
Quote:
sed -e "s/|f/;f/g" -e "s/;f1/|f1/g" file
# 3  
Old 02-21-2008
Code:
awk -F'|' ' {OFS="|";
             six="";
             for(i=6;i<=NF;i++)
                { six=six $i ";" }
             print $1,$2,$3,$4,$5,six
           }' filename

input & output:
Quote:
/home/jmcnama> cat filename
a|b|c|d|3|f1|f2|f3
a|b|c|d|5|f1|f2|f3|f4|f5
/home/jmcnama> t.sh
a|b|c|d|3|f1;f2;f3;
a|b|c|d|5|f1;f2;f3;f4;f5;
# 4  
Old 02-21-2008
Thanks Jim. It helped me in getting started. Just one followup: If the file is as:

a|b|c|d|3|f;;1;|f2;;0;|f3;;1;
a|b|c|d|5|m;;1;|x;;1;|f3;;0;|f4;;1;|f5;;1;

how can I change to

a|b|c|d|3|f;f2;f3;
a|b|c|d|5|m;x;f3;f4;f5;

Basically I need to starting from 6th column to the end of line, delete ";1;|" or ";0;|"

Thanks again for your help.

Regards

Sandeep
# 5  
Old 02-21-2008
Code:
sed -e "s/;[^|][^|]*//g" -e "s/|/;/g" -e "s/\([^;]*\);\([^;]*\);\([^;]*\);\([^;]*\);\([^;]*\);/\1|\2|\3|\4|\5|/" <file>


Last edited by sb008; 02-21-2008 at 04:48 PM..
# 6  
Old 02-21-2008
A shorter one:

Code:
sed -e 's/;[0-9].//g' -e 's/;|/;/g'

Cheers
# 7  
Old 02-22-2008
Quote:
Originally Posted by hanny
A shorter one:

Code:
sed -e 's/;[0-9].//g' -e 's/;|/;/g'

Cheers
Compliments, smart use of the double ";"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Add character to specific columns using sed or awk and make it a permanent change

Hi, I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches. file has lot of functions defined a.sh #!/bin/bash fn a { beautiful evening sunny day } fn b { } fn c { hello world .its a beautiful day ... (12 Replies)
Discussion started by: ashima jain
12 Replies

3. Shell Programming and Scripting

Removing last character of a specific line from a file

Hello guys, I would need to remove the last character ")" of a specific line. This can be from any line. Your help is appreciated. Below is the line. HOSTNAME=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)) Please help. (6 Replies)
Discussion started by: sang8g
6 Replies

4. Shell Programming and Scripting

File character adjustment based on specific character

i have a reqirement to adjust the data in a file based on a perticular character the sample data is as below 483PDEAN CORRIGAN 52304037528955WAGES 50000 89BP ABCD MASTER352 5434604223735428 4200 58BP SOUTHERN WA848 ... (1 Reply)
Discussion started by: pema.yozer
1 Replies

5. UNIX for Advanced & Expert Users

Finding a specific range of character in file

hi, I want to store from 102 character to 128 character to a variable of header record which can be identified as 'HDR' which is the first 3 characters in the same line of a same.txt file. Please advise. Thanks (4 Replies)
Discussion started by: techmoris
4 Replies

6. Shell Programming and Scripting

change character(s) in specific column

Hi all! I need to change the final e every time when it is present in any word in column 1 to a; moreover, to change the final i again to a in any word in column 1, but just if word in column 2 begins with ha or si. Here below you can see a sample of my data: achwa ungeliachwa ungeli 1... (3 Replies)
Discussion started by: mjomba
3 Replies

7. Shell Programming and Scripting

File deliminated with | character - How do I change 1 value

Hi All, I'm struggling with a problem. Data file example: Brown|32 inches|apple|monkey Green|12 feet|grape|cat Blue|32 inches|banana|dog Black|23 inches|pear|horse I need to change the value '32 inches' in the third line, and only the third line, while keeping the rest of the file the... (7 Replies)
Discussion started by: jsmith_4242
7 Replies

8. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

9. Shell Programming and Scripting

Count specific character(s) very large file

I'm trying to count the number of 2 specific characters in a very large file. I'd like to avoid using gsub because its taking too long. I was thinking something like: awk '-F' { t += NF - 1 } END {print t}' infile > outfile which isn't working Any ideas would be great. (3 Replies)
Discussion started by: dcfargo
3 Replies

10. HP-UX

count occurences of specific character in the file

For counting the occurences of specific character in the file I am issuing the command grep -o 'character' filename | wc -w It works in other shells but not in HP-UX as there is no option -o for grep. What do I do now? (9 Replies)
Discussion started by: superprogrammer
9 Replies
Login or Register to Ask a Question