how to Add word at specific location in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to Add word at specific location in line
# 1  
Old 09-09-2009
Error how to Add word at specific location in line

eg .
i have file x.txt contains :
java coding , shell scriptting etc...


now i want to add "is langauge" after word java.

output should be
java is langauge coding , shell scriptting etc...

any idea how to use shell script to do it ?
# 2  
Old 09-09-2009
if the actual requirement is always same.. then you can replace "java" with "java is language".

Code:
echo "java code" | sed 's/java/java is laguage/1'

# 3  
Old 09-09-2009
Quote:
Originally Posted by crackthehit007
eg .
i have file x.txt contains :
java coding , shell scriptting etc...


now i want to add "is langauge" after word java.

output should be
java is langauge coding , shell scriptting etc...

any idea how to use shell script to do it ?

If there's only one line in the file, there's no need for an external program:

Code:
file=x.txt
IFS= read -r line < "$file"
right=${line#*java }
left=${line%%"$right"}
echo "$left is language $right"

# 4  
Old 09-09-2009
no ....file contains multiple line of code...

but in SED solution i store the updated file at same location so if i run it twice.
i.e.
replace "java" with "java is language".

it wil screw up that file with multiple copies....

so both above solutions r not working !!

what to do ?
# 5  
Old 09-09-2009
Providing exact sample input and output your expecting will make easy to understand the problem better.
# 6  
Old 09-09-2009
File :
Quote:
hi all ,
java is lanaguage...
yes i think it is
so shell script is very powerful
java is good for platform independence projects
i want output as :
Quote:
hi all ,
java AA is lanaguage...
yes i think it is
so shell script is very powerful
java AA is good for platform independence projects
if i run ur command twice it should not effect the output. (keeping in mind..resultant file should be same as source file)
# 7  
Old 09-09-2009
In the first run a normal "sed" will solve your problem. But in the second run obviously the search pattern will be replaced since it is a successful search.

Can You tell what your real requirement is , so can check for alternates.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert character at specific location in a each line of the file

Hi All, I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script for file in /home/test/bharat/*.RET do # Process file echo "File Name=" $file #l_fileName="${file##*/}" ... (19 Replies)
Discussion started by: bharath561989
19 Replies

2. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

3. Shell Programming and Scripting

Deleting lines in a fixed length file where there is a word at specific location

I have a big file having 100 K lines. I have to read each line and see at 356 character position whethere there is a word "W" in it. If it is their then don't delete the line otherwise delete it. There are two lines as one Header and one trailer which should remain same. Can somebody... (5 Replies)
Discussion started by: mohit kanoongo
5 Replies

4. Shell Programming and Scripting

How to find a word and move it a specific location in xml file using perl?

Hi friends, I have one XML file having below structure :- INput XML file :- <?xml version="1.0" encoding="UTF-8"?> <START> <A=value1> <attr name1="a1"> </A> <B=value2> <attr name2="b1"> <attr name3="c1"> </B> </START> output xml file should be === (3 Replies)
Discussion started by: harpal singh
3 Replies

5. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

6. Shell Programming and Scripting

Insert text line to specific location CSV

In Perl. ***edited question below*** Hey all, I am teaching myself some simple CSV file manipulation and have become a little stuck. Say I have the following layout in the CSV file: age,name,locationIs it possible to INSERT data into the CSV into the correct age order. For example, if I had... (1 Reply)
Discussion started by: whyte_rhyno
1 Replies

7. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

8. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

9. Shell Programming and Scripting

How to append a string to a specific location in a line

Hi, I have a to modify a line and insert a keyword in the middle to a specific location. My line looks like this FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (ABC, DEF, GHI) I want to change it as FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING... (4 Replies)
Discussion started by: mwrg
4 Replies

10. Shell Programming and Scripting

Finding a word at specific location in a string

Hi All , I have different strings (SQL queries infact) of different lengths such as: 1. "SELECT XYZ FROM ABC WHERE ABC.DEF='123' " 2. "DELETE FROM ABC WHERE ABC.DEF='567'" 3. "SELECT * FROM ABC" I need to find out the word coming after the... (1 Reply)
Discussion started by: swapnil.nawale
1 Replies
Login or Register to Ask a Question