Write a word at 72nd position of a matched line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Write a word at 72nd position of a matched line in a file
# 1  
Old 10-25-2013
Write a word at 72nd position of a matched line in a file

Hi,

I need to search a file for a pattern,replace some other word and write a word at its 72nd position.

For example,

My name is Mano.Im learning Unix.

I want to search the file in all lines containing the word "Mano".In that matched line,replace the word "Unix" with "Java".And write the string "Line Changed" at its 60th position.

output like,

My name is Mano.Im Learning Java. Line Changed

Kindly help
# 2  
Old 10-25-2013
Quote:
Originally Posted by mano1 n
Hi,

I need to search a file for a pattern,replace some other word and write a word at its 72nd position.

For example,

My name is Mano.Im learning Unix.

I want to search the file in all lines containing the word "Mano".In that matched line,replace the word "Unix" with "Java".And write the string "Line Changed" at its 60th position.

output like,

My name is Mano.Im Learning Java. Line Changed

Kindly help
Not sure what exactly what you want.. for given input following will work

try

Code:
$ echo "My name is Mano.Im learning Unix" | awk '/Mano/{gsub("Unix","Java")}1'
My name is Mano.Im learning Java

OR

Code:
$ echo "My name is Mano.Im learning Unix" | sed '/Mano/s/Unix/Java/g'
My name is Mano.Im learning Java

OR
Code:
$ echo "My name is Mano.Im learning Unix" | perl -pe '/Mano/ && s/Unix/Java/g'
My name is Mano.Im learning Java


Last edited by Akshay Hegde; 10-25-2013 at 08:38 AM..
# 3  
Old 10-25-2013
Thanks Akshay.

After Finding and replacing, in the same matched line , i want to put a comment,say "Line Changed" , at its nth position .
The output should be,

My name is Mano.Im learning Java Line Changed.
# 4  
Old 10-25-2013
Try

Code:
$ echo "My name is Mano.Im learning Unix" | awk '/Mano/{gsub("Unix","Java");printf $0 OFS "Line Changed.\n"}'
My name is Mano.Im learning Java Line Changed.

# 5  
Old 10-25-2013
Hi mano1 n,

Could you please try the following code, this wil help too.


Code:
echo "My name is Mano.Im learning Unix" | sed '/Mano/s/Unix/Java Line Changed/g'

It's output will be as follows.

Code:
My name is Mano.Im learning Java Line Changed


Thanks,
R. Singh
# 6  
Old 10-25-2013
Not sure why you say pos. 72 in the thread header and 60 in your first post. And, had you used code tags as required by forum rules, your desired output had been clear to all other posters above. Nevertheless, try:
Code:
awk '/Mano/  {sub("Unix","Java"); printf "%-59s%s\n", $0, "Line Changed."}' file
My name is Mano.Im learning Java.                          Line Changed.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Write a script for getout afew line and grep a define word

dear all Hi i want write a script can count number of my log file and every 5min run script for cgk log file for find a specific word such as Error for example in first 5 min we have 500 line in my log file and in second 5min we have 2500 line in my log file how can chk error word in my log... (4 Replies)
Discussion started by: Baber
4 Replies

2. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

3. Shell Programming and Scripting

Regex to identify word in second position on a line

I am interested in finding a regex to find a word in second position on a line. The word in question is या I tried the following PERL EXPRESSION but it did not work: ] या or ^\W या But both gave Null results I am giving below a Sample file: देना या सौंपना=delegate तह जमना या... (8 Replies)
Discussion started by: gimley
8 Replies

4. Shell Programming and Scripting

Match a line in File 1 with Column in File 2 and print whole line in file 2 when matched

Hi Experts, I am very new to scripting and have a prb since few days and it is urgent to solve so much appreciated if u help me. i have 2 files file1.txt 9647810043118 9647810043126 9647810043155 9647810043161 9647810043166 9647810043185 9647810043200 9647810043203 9647810043250... (22 Replies)
Discussion started by: mustafa.abdulsa
22 Replies

5. UNIX for Dummies Questions & Answers

Script to delete a word based on position in a file

Hi, I am new to unix. I want to delete 2 words placed at position say for example at 23rd and 45th position in a line. I used sed but couldnt achieve this. Example: the file contains 2 lines 12345 98765 "12345" 876 12345 98765 "64578" 876 I want to delete " placed at position 13 and 19... (4 Replies)
Discussion started by: nbks2u
4 Replies

6. Shell Programming and Scripting

How to put a word starting at particular position in a file using shell scripting

Hi all, I'm new to shell scripting and hence this query. I have 2 files. temp.txt and config.txt. The values in temp.txt are tab separated. ex: temp.txt AB CDE GHIJ OPQRS WXY ex:config.txt (1st line for 1st element of temp.txt and so on) start = '1' end='5' start = '6' end =... (26 Replies)
Discussion started by: subhrap.das
26 Replies

7. UNIX for Dummies Questions & Answers

Paste a word in the third field position of a file

Hi All, I have a file like this, 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 The above line is a comma separted data file. I want to modify the third field to The final data file should be like 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 ... (1 Reply)
Discussion started by: girish.raos
1 Replies

8. Shell Programming and Scripting

How to write the file by position

I have 100000 records in excel spreed sheet . How do we write that into dat files based on position.If spread sheet contain null then we need as space..For example abc 123456 dsjfjkl abcde 123 ccddde ..... ......... .. 1st column should have data from 1-7 then 2nd column from... (1 Reply)
Discussion started by: ford2020
1 Replies

9. Shell Programming and Scripting

How to check a word position in a file ?

Hello everybody, I have a file like this : "window 1 truck 3 duck 2... fire 1... etc..." and I would like to print the following number of a word I am searching for. (For example here, if I search for the word "fire", I will print "1") Thank you for your help ! (7 Replies)
Discussion started by: tibo
7 Replies

10. Shell Programming and Scripting

How to get a next line of a matched word?

Hi , If I match a word in a file, I want to pick the next line of that matched word. My file is a below format- The ntrag trace has auditError Line5005 contains transaction Ntrag data ------------ Here I wanted if I match a word auditError, I need to get the next line "Line5005... (10 Replies)
Discussion started by: Muktesh
10 Replies
Login or Register to Ask a Question