positioning words in a line


 
Thread Tools Search this Thread
# 1  
positioning words in a line

I have problem with this for last few days and I would really be grateful if you can help me SmilieSmilieSmilieSmilieSmilie

I have sets of files in one directory, and each of them have few lines
each one of these lines has 1 or more occurrence of words
Y = [Dean James John Mark Bob]
I need to find the position of the Y in each line and put them into a file with number of the words in the whole line and name them $Y-$name
e.g. one file from directory --> file01.txt
this is the house for James and
Mark and Dean would like to
James want to test
Results
1) Jame-file01.txt
67
14
2) Mark-file01.txt
16
3) Dean-file01.txt
36

I have tried to do bits as well which is not helpful either
I have tried to put each line in a file and then break based on space and then get the line number as position using AWK (not very useful way of dealing as I end up with millions of files) and then I have tried following lines which is not working correctly either as they dont detect the names
Code:
FILES="F1/*"
for X in $FILES
do
	name=$(basename $X) 
	for Y in Dean James John Mark Bob
	do
		grep –o "$Y" | awk  {print $1,  NR }'  $X > position/$Y-${name}
	done
done

can you please help me in this?
# 2  
I don't know what you mean by getting millions of files with AWK... Here is the AWK solution that generates just what you requested:
Code:
for i in Dean James John Mark Bob; do awk -vx=$i '$0~x{for (i=1;i<=NF;i++) if (x==$i) print i,NF > x"-"FILENAME}' file01.txt; done

# 3  
Try this..Smilie

Code:
for file in *.txt
do
for i in $(cat file1)
do
awk -v VM="$i" -v NM="$file" '{ for (i=1;i<=NF;i++) { if ($i ~ VM ) { print i, NR >> VM"-"NM }}}' $file
done
done

# 4  
@bartus: i was geting may files as my approach was wrong (that was what I meant Smilie )
and now that I tried your line i get the following error... dont know where the problem is
Quote:
awk: (FILENAME=F1/Dean-Dean FNR=1) fatal: can't redirect to `and-F1/To-to' (No such file or directory)
@Pamu: what is file1 here?
if I am getting this right, I am calling the directroy in the first line and then I would need to call my parameters Y = [Dean James John Mark Bob]
can I do this you think?
Code:
for file in *.txt
do
        awk -v VM= "/^Dean$|^James$|^John$|^Mark$|^Bob$/"  -v NM="$file" '{ for (i=1;i<=NF;i++) { if ($i ~ VM ) { print i, NR >> VM"-"NM }}}' $file
done


Last edited by A-V; 09-11-2012 at 10:50 AM..
# 5  
What is the exact command that you are using (variables and filename)?
# 6  
I have a whole folder which they have pattens of 5 words of Y= [Dean James John Mark Bob], so I have 25 files with pattern of $Y-$Y
now in each one of these $Y-$Y i have lines that again which will again have $Y
now I need to get the pattern as $Y-$Y-$Y with will have only two columns
$1 = position of the words $Y
$2 = number of the words in each line

As I am new, I was dividing each line to a file and then break each word to new file and then get NR --- i know its funny but im learning Smilie
Code:
FILES="F1/*"
for X in $FILES
do
    name=$(basename $X) 
    awk -v NM="$name" '$0 != ""{ print > "F2/"NM"-"NR".txt" }' $X 
done
#word per line separator 
FILES="F2/*"
for X in $FILES
do
	name=$(basename $X) 
	cat $X | tr ‘ ‘ ‘\n’ >F3/${name}
done
#get the position and count the number of lines per file
FILES="F3/*"
for X in $FILES
do
	name=$(basename $X) 
awk  '/^Dean$|^James$|^John$|^Mark$|^Bob$/  {print $1,  NR }'  $X > F4/${name}
awk  'END{print NR-1}'  $X > F5/${name}
done

this is a mess and those not really do what I need for the naming which is adding the third parameter as well and has many separate files which need to be added up later...
I am looking for more appropriate way of doing it

@Pamu: I used used as its the same problem Smilie Smilie

Last edited by A-V; 09-11-2012 at 11:42 AM..
# 7  
Bug

Quote:
Originally Posted by A-V
@bartus: i was geting may files as my approach was wrong (that was what I meant Smilie )
and now that I tried your line i get the following error... dont know where the problem is


@Pamu: what is file1 here?
if I am getting this right, I am calling the directroy in the first line and then I would need to call my parameters Y = [Dean James John Mark Bob]
can I do this you think?
Code:
for file in *.txt
do
        awk -v VM= "/^Dean$|^James$|^John$|^Mark$|^Bob$/"  -v NM="$file" '{ for (i=1;i<=NF;i++) { if ($i ~ VM ) { print i, NR >> VM"-"NM }}}' $file
done

hi

file1 is having your words such as James, dean and so on..
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

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 multiple file line by line

Hi All I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Discussion started by: netdbaind
4 Replies

3. Shell Programming and Scripting

get few words from same line

Hello all i tried awk , cut but i think something missing i have this line @@XYMONDCHK-V1|.acklist.|developer_instead|rendy_google_yagom|1323977582|1323979382|1323979382|1|admin|test case i want cut some words to be in new line like this... (6 Replies)
Discussion started by: mogabr
6 Replies

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

5. Shell Programming and Scripting

How to print the words in the same line with space or to the predefined line?

HI, cat test abc echo "def" >> test output is cat test abc def the needed output is cat test abc def and so on (5 Replies)
Discussion started by: jobycxa
5 Replies

6. Shell Programming and Scripting

sort words in a line

Hi Im looking for a way, hopefully a one-liner to sort words in a line e.g "these are the words in a line" to "a are in line the these words" Thanks! (15 Replies)
Discussion started by: rebelbuttmunch
15 Replies

7. Shell Programming and Scripting

print only last two words of a line

can u help me out to print last two words of each sentence of a file. for example. contents of input file: i love songs my favourite songs sent songs all kind good buddy Ouput file should contain: love songs favourite songs sent all kind good buddy (5 Replies)
Discussion started by: pradeepreddy
5 Replies

8. Shell Programming and Scripting

how to get line number of different words if exists in same line

I have some txt files. I have to create another text file which contains the portion starting from the format "Date Sex Address" to the end of the file. While using grep -n on Date it also gives me the previous line containg Date. and also Date may be DATE in some files. My file is like this... (10 Replies)
Discussion started by: Amiya Rath
10 Replies

9. Shell Programming and Scripting

count no of words in a line

hi i have a string like str=abc def ghi jkl now i want to count the no of words in the string please help (7 Replies)
Discussion started by: satish@123
7 Replies

10. Shell Programming and Scripting

seperating the words from a line??

The line is like this +abc+def+mgh+ddsdsd+sa i.e. words seperated by +. There is a plus in the beginning. i want to conver this line to abc, def, mgh, ddsdsd, sa please provide the logic in the form of a shell script Thanks in advance (3 Replies)
Discussion started by: skyineyes
3 Replies

Featured Tech Videos