To Extract words from File based on Position


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To Extract words from File based on Position
# 1  
Old 02-18-2010
To Extract words from File based on Position

Hi Guys,

While I was writing one shell script , I just got struck at this point.

I need to extract words from a file at some specified position and do some comparison operation and need to replace the extracted word with another word.

Eg : I like Orange very much.

I need to replace the word starting from 8th position till 15th ( Here 'Orange ') and needs to replace it with 'Apple ' .

I am looking for a command for this.

Will Sed command work if the word position is the only known parameter?
Looking for your suggesions

Thanks
# 2  
Old 02-18-2010
You can try it with awk...
Code:
echo "I like Orange very much." | awk '{val=substr($0,8,6);
sub(val,"Apple",$0);print $0}'

# 3  
Old 02-19-2010
Thank You Malcome. I will try and get back .
# 4  
Old 02-27-2010
Thank You Malcom. This looks good.

But One quick question what is the command to display characters b/w 5 to 10 for all the records in a file.
# 5  
Old 02-27-2010
Code:
awk '{val=substr($0,5,6);print val}' infile

# 6  
Old 02-27-2010
Code:
echo "hai this is for testing" | cut -b 4-7.

You can also use the above command to extract a range of characters from the string or from input file

---------- Post updated at 02:51 PM ---------- Previous update was at 02:38 PM ----------

Code:
IFS="\n"
for line in `cat input_file`
do
        echo ${line:5:5}
done

And also the above script will extract the characters from the 5th position up to the next 5 characters. Which means that it is extracting from 5 to 10 characters
# 7  
Old 02-27-2010
For extracting and replacing the specified characters from a file or string,you can use the following command.

Code:
echo "I like grape very much" | sed -rn 's/^(.{7}).{5}(.*)/\1Apple\2/gp'

The above statement will replace the characters from 7th position 12th position in the string "I like grape very much".It will give the output as follows.

I like Apple very much

If you want to replace it in the file,you use the following command.

Code:
sed -rni 's/^(.{7}).{5}(.*)/\1Apple\2/gp' <filename>

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

3. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

4. Shell Programming and Scripting

Split file based on distinct value at specific position

OS : Linux 2.6x Shell : Korn In a single file , how can I identify all the Uniqe values at a specific character position and length of each record , and simultaneously SPLIT the records of the file based on each of these values and write them in seperate files . Lets say : a) I want to... (4 Replies)
Discussion started by: kumarjt
4 Replies

5. Shell Programming and Scripting

Extract substring specif position and length from file line

Hi gurus, I am trying to figure out how to extract substring from file line (all lines in file), as specified position and specified legth. Example input (file lines) dhaskjdsa dsadhkjsa dhsakjdsad hsadkjh dsahjdksahdsad sahkjd sahdkjsahd sajkdh adhjsak I want to extract substring on... (5 Replies)
Discussion started by: ProsteJa
5 Replies

6. Shell Programming and Scripting

Put words to fix position in a file

Hi all, There are several lines in my file as a=123,b=dene,c=2312,d=234234,g=vxcvxcv,h=44 a=3,b=dene,c=22,d=23422342334,g=vxcvxcv,h=4 a=123,b=dene,c=2312,d=234234,g=vxcvxcv,h=678 I take values with this command awk -F '' '{print $1,$2,$3}' a.txt I want to put values to a fix position... (6 Replies)
Discussion started by: bahadiraktan
6 Replies

7. UNIX for Dummies Questions & Answers

extract regions of file based on start and end position

Hi, I have a file1 of many long sequences, each preceded by a unique header line. file2 is 3-columns list: headers name, start position, end position. I'd like to extract the sequence region of file1 specified in file2. Based on a post elsewhere, I found the code: awk... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

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

9. Shell Programming and Scripting

Extract data based on position

The file has record length 200. And i have 100 search strings which are ten digits of character from 1 to 10 characters all of them are unique, they need to searched in a file. Please help me to pull the records based on position (say from 1-10). test data 1FAHP2DW0BG115206RASHEED ... (6 Replies)
Discussion started by: zooby
6 Replies

10. UNIX for Dummies Questions & Answers

Extract words to new file

Hi there, Unix Gurus Working with big listings of english sentences for my pupils, of the type: 1. If the boss's son had been , someone would have asked for money by now. 2. Look, I haven't a crime, so why can't you let me go? .... I wondered how to extract the words between brackets in... (7 Replies)
Discussion started by: eldeingles
7 Replies
Login or Register to Ask a Question