Adding words after a set of words


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding words after a set of words
# 1  
Old 04-08-2010
Adding words after a set of words

Greetings.

I am a UNIX newbies.

I am currently facing difficulties dealing with a large data set and I would like to ask for helps. I have a input file like this:

ak 1 AAM1
ak 2 AAM1
ak 3 AAM1
ak 11 AMM2
ak 12 AMM2
ak 13 AMM2
ak 14 AMM2

Is there any possibility for me to have an output like:
SET1
ak 1 AAM1
ak 2 AAM1
ak 3 AAM1
END
SET2
ak 11 AMM2
ak 12 AMM2
ak 13 AMM2
ak 14 AMM2
END
...

It is too complicated for me and I do not know which language to use too. Please give me your opinion. Thank you very much.

Amanda
# 2  
Old 04-08-2010
try out this..


Code:
for i in `cat <file_name>| awk '{print $3}' | sort | uniq`; do echo "SET";  echo $i | sed -e "s/^.*\(.\)$/\1/"; grep $i testing;echo "END";done

<file_name> -- original file

---------- Post updated at 07:04 AM ---------- Previous update was at 06:59 AM ----------

Try this one

Code:
for i in `cat testing | awk '{print $3}' | sort | uniq`; do a="SET"; b=`echo $i | sed -e "s/^.*\(.\)$/\1/"`;echo $a$b; grep $i testing;echo "END";done

# 3  
Old 04-08-2010
Thank you. It is very helpful!

However, probably I was not careful for giving the input and output file format, I am sorry. If I would like to have the SET in continuous number, such as SET1, SET2, SET3, and so on, which is not following the last letter of the second string, would that be possible?

Thank you once again for your reply.
# 4  
Old 04-08-2010
please provide a valid example

please provide a better example of what is your input and output. I guess kamaraj has given the best solution to what you asked.
vshar
# 5  
Old 04-08-2010
Yes, the code is very helpful, however when I applied the code to the large dataset, the output file is like below:

SETG
ak 126 A1BG
ak 893 A1BG
END
SETF
ak 109 A1CF
ak 127 A1CF
ak 169 A1CF
END
SET1
ak 101 A2BP1
ak 106 A2BP1
ak 103 A2BP1
ak 107 A2BP1
...

Hence, I was just wonder whether the SET on top on each word set can be in continuous manner such as:
SET1
ak 126 A1BG
ak 893 A1BG
END
SET2
ak 109 A1CF
ak 127 A1CF
ak 169 A1CF
END
SET3
ak 101 A2BP1
ak 106 A2BP1
ak 103 A2BP1
ak 107 A2BP1
...

I will be more careful in giving the example next time. Thank you once again.
# 6  
Old 04-08-2010
try this

b=0;for i in `cat testing | awk '{print $3}' | sort | uniq`; do a="SET"; b=`expr $b + 1` ;echo $a$b; grep $i testing;echo "END";done
vshar
# 7  
Old 04-08-2010
The code is working. Thank you very much for both of your effort. Excellent!

Thanks again.
 
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 a column a return a set of words

Hi I have two files. One is a text file consisting of sentences i.e. INPUT.txt and the second file is SEARCH.txt consisting of two or three columns. I need help to write a script to search the second column of SEARCH.txt for each set of five words (blue color as set one and green color as set... (6 Replies)
Discussion started by: my_Perl
6 Replies

4. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

5. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

6. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

7. Shell Programming and Scripting

Editing in vi - adding words to a line

i have lines in a file similar to this: results=$(echo Total: ${res} | command | command) now I need to add text before the word "results", but i dont know how to do it. here's what i tried: %s~results=.*echo Total:.* ${7}~PROCESS \; results=.*echo Total:.* ${7}~g the problem is,... (1 Reply)
Discussion started by: SkySmart
1 Replies

8. Shell Programming and Scripting

Adding numbers matching with words

Hi All, I have a file which looks like this: abc 1 abc 2 abc 3 abc 4 abc 5 bcd 1 bcd 3 bcd 3 bcd 5 cde 7 This file is just a miniature version of what I really have. Original file is some 1 million lines long. I have tried to come up with the code for what I wish to accomplish... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

9. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

10. Shell Programming and Scripting

Adding words to beginning of lines

I have a file that contains a great number of lines, let's say 183 lines, and I want to add: echo " to the beginning of each line. What is the easiest way to do it? Tx (9 Replies)
Discussion started by: Ernst
9 Replies
Login or Register to Ask a Question