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
# 8  
Old 02-27-2010
Guys, Really appreciate your quick response.!!

I am a beginer in the Unix area, thats may b reason i still could nt get solve the problem Smilie

This is the sample of the code I have written. But its not working correctly.

The substitution part works good. But I would like to add 2 more display statements.

Display the Original strings & those got replaced. But thats not coming correctly.I tried with both for loop & awk as suggested by you guys.

But not working.

Please advice.

Sample code :--

Code:
#/bin/ksh
echo "Enter the file name "
read input_file
echo "This is the Contents "
cat $input_file
#IFS="\n"
for line in `cat input_file`
do
echo ${line:1:3}
done
#awk '{val=substr($0,1,3);print val}' input_file
sed -e 's/ABC /XYZ /' -e 's/DEF /ABCD /' <$input_file >$output_file
cat $output_file
echo "Bye"


Last edited by Scott; 02-27-2010 at 02:35 PM.. Reason: Code tags please...
# 9  
Old 02-27-2010
Hi I do not know what exactly isn't working, but I changed your code so that it is providing output as I figured you intended with your script (not your original specification, since this seems to be quite different).

You can take it from there. Otherwise perhaps you could be more specific about what isn't working.

Code:
#!/bin/ksh
echo "Enter the file name "
read input_file
echo "This is the Contents "
cat "$input_file"
output_file="${input_file}.out"
while read line
do
  echo "${line:0:3}"
done < "$input_file"
sed -e 's/ABC /XYZ /' -e 's/DEF /ABCD /' <$input_file >$output_file
cat "$output_file"
echo "Bye"

# 10  
Old 02-28-2010
Hi,

Thanks for the response.

Still Getting an error at the string display part!!

I will summarise what I am trying to do. Sorry if I had confused anyone.
I am pasting the in_file & out_file for your referece.

Please help.

This is my problem:--->

In_file

I like Orange Very Much
I like Mango also
We are Grape lovers.
We play Cricket usually
We are Chess players

The out_file needs to look like this

I like Apple Very Much
I like Apple also
We are Grape lovers.
We play Football usually
We are Football players


Task for Shell script :---

1 Read in_file --> user input
2 Get out_file name --> user input
3 Display the strings b/w coulmn 9 to 17 in the in_file for all the records.


Changes
--------
4 If Orange/Mango comes in the specified postion need to change as Apple.


5 If Cricket/Chess comes in the specified postion ,need to replace as Football and save in the out_file

6 Display the strings b/w coulmn 9 to 17 in the output file

( Like this i have many changes. I am using a single SED command for the task, It is working fine(but its just a pattern matching one, not checking position, I would like to add that feature also) . But problem is the string display portion is not working )



Thanks
# 11  
Old 02-28-2010
What do you mean with "b/w" ?
If I display colums 9-17 I get:
Code:
$> cut -c9-17 infile
range Ver
ango also
rape love
Cricket u
hess play

Do you mean 8-16:

Code:
$> cut -c8-16 infile
Orange Ve
Mango als
Grape lov
 Cricket
Chess pla

So the original lines with the words replaced only if they happen to fall into these columns?
# 12  
Old 02-28-2010
If i get you correctly, this will do what you want and you can take it from here:

Code:
#!/bin/ksh
echo "Enter Input file"
read infile
while read line
do
wd=$(echo "${line}" | cut -c 9-17)
echo "The word in the specified position is: ${wd}"
echo "Enter the word to change it with:"
read wrd < /dev/tty
changed=$(echo "${line}" | sed 's/'"${wd}"'/'"${wrd}"'/')
echo ${changed} #> output.out
done < "${infile}"

# 13  
Old 02-28-2010
Hi

b/w is short form of 'between'......

Starting position of the String is where the words like Orange/Mango/cricket etc.. (starting position is 8 I guess...and from there next 7 characters )

i/p file data :--->

I like Orange Very Much
I like Mango also
We are Grape lovers.
We play Cricket usually
We are Chess players

o/p file data needed -->

I like Apple Very Much
I like Apple also
We are Grape lovers.
We play Football usually
We are Football players


I need the original lines with the words replaced in the output file if they happen to fall into these columns.

but would like to display the portions replaced alone also . This for only display purpose.

---------- Post updated at 02:51 AM ---------- Previous update was at 02:43 AM ----------

Malcom..


Seems your code is fine..Thanks..I will confirm with in 15 mins.

Thanks

---------- Post updated at 03:37 AM ---------- Previous update was at 02:51 AM ----------

Malcome,

Your script is working fine if both strings ( Original and replaced part )
are of identical size.

ie It is working fine If am replacing 'Orange' (6 leter ) by another 6 leter word (say 'apples').

But If am replacing a 7 letter word ( say 'Cricket' ) with a 4 letter word ( say 'Golf' ) then its not working fine. It will replace the word .But position of next column changes.

This is wat i need -->

If a 7 leter word is replaced by a 5 leter word, then next 2 positions will be 'SPACES' so that the position of next column remains unchanged.


I guess u got my problem.
# 14  
Old 02-28-2010
What happens when a 4 letter word is replaced by a 7 letter word?
 
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