How to insert at a particular position in flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert at a particular position in flat file
# 1  
Old 08-07-2007
How to insert at a particular position in flat file

Hi All,
I have a flat file with ~ as de-limiter (e.g: aaa~ba a~caa~0~d~e)
What I want is check if the 4th character is 0 and replace it with say 4. So now it becomes : aaa~ba a~caa~4~d~e.

I have to do this for the whole file, but the delimiter position remains the same, not the character length.

Please help.
Abhi.
# 2  
Old 08-07-2007
Try this..

Code:
awk -F"~" '{if ($4==0) $4=4; print $0;}' filename

# 3  
Old 08-07-2007
To start:
Code:
pattern=$1
rep_string=$2
awk -F\~ -v pat=${pattern} -v rep=${rep_string} 'BEGIN{OFS=FS}{if ( $4 == pat){sub(/'"${pattern}"'/,rep,$4)}print}' txt


Last edited by Klashxx; 08-07-2007 at 05:59 AM..
# 4  
Old 08-07-2007
cat filename | cut -d "~" -f 4|sed -e 's/0/4/'
# 5  
Old 08-07-2007
Code:
awk -F'~' 'BEGIN{OFS="~";}{ if ($4 != 4){$4=4;} print }' your_file

As per the req it should have been like

Code:
awk -F'~' 'BEGIN{OFS="~";}{ if ($4 == 0){$4=4;} print }' your_file


Last edited by lorcan; 08-07-2007 at 06:06 AM.. Reason: Changing as per req
# 6  
Old 08-07-2007
Quote:
Originally Posted by jacoden
Try this..

Code:
awk -F"~" '{if ($4==0) $4=4; print $0;}' filename

Hi Jacoden

I tried it , but i have lost my de-limiters '~' and got only space instead.

Abhi.
# 7  
Old 08-07-2007
Quote:
Originally Posted by lorcan
Code:
awk -F'~' 'BEGIN{OFS="~";}{ if ($4 != 4){$4=4;} print }' your_file

Thanks a lot this works.

Abhi.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to insert a '#' in the first position of all the files?

Hi All, how to insert a '#' in the first position of all the files based on a certain condition. I tried this: cat /bin/user/input_file.txt | while read a do b=`sed 's/.*song=good.*/\#&/g' $a ` echo $b > /bin/user/new/output_file.txt done input_file.txt has list of names of 10... (5 Replies)
Discussion started by: anand787
5 Replies

2. Shell Programming and Scripting

Insert empty columns in a flat file

Hi, I have a tab delimited flat file, for example shown below Name Desg Loc a b c d e fI want to insert an empty column inbetween the column Desc and Loc, the result should be like shown below: Name LName Desg Loc a b c d e ... (6 Replies)
Discussion started by: sampoorna
6 Replies

3. Shell Programming and Scripting

Insert | in specific position

Hi , I have a file which has line similar to below 13123324234234234234234234234234234 3454546456dfhgfhgh454645654asdasfsdsddfgdgdfg 345345345mnmnbmnb346mnb4565464564564645645 Not for each line for specific position I need to insert some '|' Positions are fixed. Like 3,5,9,11 So the... (5 Replies)
Discussion started by: Anupam_Halder
5 Replies

4. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

5. UNIX for Advanced & Expert Users

Insert Delimiter at fixed locations in a flat file

Hi Can somebody help me with solution for this PLEASE? I have a flat file and need to insert delimiters at fixed positions in all the lines so that I can easily convert into EXCEL with columns defined as per their width. For Example Here is the file { kkjhdhal sdfewss sdtereetyw... (7 Replies)
Discussion started by: jd_mca
7 Replies

6. Shell Programming and Scripting

Insert file contents into another file at a given position

I'm trying to write a small shell script/command to insert the contents of one file into another file at a position marked with a given text string. It's not necessarily at the top or bottom of the file so I can't just use cat to cat the files together. I think probably sed with the r option is... (5 Replies)
Discussion started by: shaun29
5 Replies

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

8. Shell Programming and Scripting

Convert case on specified position of flat file

Please help Need a script which will do the following : Search on fixed width file , go to position (25,2) which means 25th and 26th position, Find if there are any char in lower case: For example 25,2 can be (9T) or (9w) or (Ww) or (wW)....The two positions can be numeric or alpha...no... (13 Replies)
Discussion started by: ssantoshss
13 Replies

9. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies

10. HP-UX

find the position in a file and insert the data there

Hi, I have a EDI data file ARROWTEST of size 18246 characters. And I want to insert some data after 4200 position in the file. How I can find the position 4200 in that file....Please advise. Regards, (5 Replies)
Discussion started by: isingh786
5 Replies
Login or Register to Ask a Question