LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2011, 11:32 PM   #1
nobtiba
Member
 
Registered: Jan 2007
Posts: 40

Rep: Reputation: 0
How to add characters and lines to file


Dear Bros,

I have this issue but haven't figure out the solution:
For example, I have a file called "file" like this one:
type=strongsubj len=1 word=absolve pos=verb stemmed=y priorpolarity=positive
type=strongsubj len=1 word=unique pos=adj stemmed=n priorpolarity=neutral
type=strongsubj len=1 word=absolutely pos=adj stemmed=n priorpolarity=neutral
type=weaksubj len=1 word=taking pos=verb stemmed=y priorpolarity=positive
type=weaksubj len=1 word=friend pos=noun stemmed=n priorpolarity=positive
type=weaksubj len=1 word=usually pos=adverb stemmed=n priorpolarity=positive
type=strongsubj len=1 word=purecolor pos=anypos stemmed=n priorpolarity=negative
type=strongsubj len=1 word=accusingly pos=anypos stemmed=n priorpolarity=negative

I want to add the plural for the noun, for example if find this line:
type=weaksubj len=1 word=friend pos=noun stemmed=n priorpolarity=positive
will add one more line :
type=weaksubj len=1 word=friends pos=noun stemmed=n priorpolarity=positive
where we add "s" for the word friend
I did try to do like that:
<code>
cat file | while read LINE ; do

set -- ${line}
if [[ "${4#pos1=}" == "noun" ]];then
#I tried this line but it doesn't work properly:
v3==$(echo $line |sed 's/$3/$s') #I want to find the third word "word=friend" in that line and add "s" after that word
# I don't know what command to add this new line "$v3" to the file ???

done
</code>

Could you please give me some hint ?
 
Old 01-30-2011, 12:02 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

There are two questions here:
1. How do you parse the input line (e.g. in order to intelligently find "friend" and change it to "friends")?

2. How do you add the new line to the end of the file?

It sounds like you've actually figured out "1)", the hard part. You implied you're getting a valid string in "$v3". If so, cool! If not, I'd urge you to consider exploring "awk". Or writing the whole thing in Perl or Python.

The easy part is "2)":
Code:
echo $v3 >> myfile
 
Old 01-30-2011, 12:04 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,278

Rep: Reputation: 5346Reputation: 5346Reputation: 5346Reputation: 5346Reputation: 5346Reputation: 5346Reputation: 5346Reputation: 5346Reputation: 5346Reputation: 5346Reputation: 5346
Here's a Python solution:

Code:
word_index = 2
pos_index = 3
output = []
with open("file") as f:
    for line in f:
        output.append(line)
        tokens = line.split()
        pos = tokens[pos_index].split('=')[1]
        if pos == 'noun' and tokens[word_index][-1] != 's':
            tokens[word_index] = tokens[word_index] + 's'
            output.append(' '.join(tokens))

for line in output:
    print line
 
Old 01-30-2011, 02:40 AM   #4
MALCOMEX999
LQ Newbie
 
Registered: Aug 2009
Posts: 1

Rep: Reputation: 1
With awk...

Code:
awk '{split($4,noun,"=");if(noun[2]="noun"){$3=$3"s"}}1' infile
 
1 members found this post helpful.
Old 01-30-2011, 02:59 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,012

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Again with awk:
Code:
awk -F"[ =]" '$8 == "noun"{$6=$6"s"}1' file
 
1 members found this post helpful.
Old 01-30-2011, 10:22 PM   #6
nobtiba
Member
 
Registered: Jan 2007
Posts: 40

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Again with awk:
Code:
awk -F"[ =]" '$8 == "noun"{$6=$6"s"}1' file
@MALCOMEX999: I tried your solution but it seems to add "s" for every word.
@grail: your solution look very short and work... I dont really understand the "awk" command so I dont know how to add this to my script? (or it work alone ?)
This:
=====================================================
cat file | while read LINE ; do
set -- ${line}
awk -F"[ =]" '$8 == "noun"{$6=$6"s"}1' file
done
=====================================================
give me infinite loops, never stop.
What I want is after double the line it found (find the noun then copy a new line below it) then modify it (add "s"). Finish. When we open that file, we should able to see the new line with addition.
Will this work ?
 
Old 01-31-2011, 01:06 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,012

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
No you don't need the bash while loop and so on as it will stand alone.
Easiest way to do what you are asking is:
Code:
awk -F"[ =]" '$8 == "noun"{line=$0;$6=$6"s";$0=line"\n"$0}1' file
 
1 members found this post helpful.
Old 02-01-2011, 07:16 AM   #8
nobtiba
Member
 
Registered: Jan 2007
Posts: 40

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
No you don't need the bash while loop and so on as it will stand alone.
Easiest way to do what you are asking is:
Code:
awk -F"[ =]" '$8 == "noun"{line=$0;$6=$6"s";$0=line"\n"$0}1' file
Your solution give the right result but it only print to the screen, not write to the file it self, so it will be hard for me to copy only the line with "s" to the file manually.
Is there any way to solve ?
 
Old 02-10-2011, 08:49 PM   #9
nobtiba
Member
 
Registered: Jan 2007
Posts: 40

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
No you don't need the bash while loop and so on as it will stand alone.
Easiest way to do what you are asking is:
Code:
awk -F"[ =]" '$8 == "noun"{line=$0;$6=$6"s";$0=line"\n"$0}1' file
Hi grail,
its my fault not to see the result carefully. Yes, it add one line with modification as expected !
Thats great job !
Can this command do more than one modification ? For example, not only add "s" after the "noun" but also add "s" after verb, "ed" after verb.
for example if we have this:
type=weaksubj len=1 word1=abandonment pos1=noun stemmed1=n priorpolarity=negative
type=weaksubj len=1 word1=abandoned pos1=adj stemmed1=n priorpolarity=negative
type=weaksubj len=1 word1=abandon pos1=verb stemmed1=y priorpolarity=negative
type=strongsubj len=1 word1=abase pos1=verb stemmed1=y priorpolarity=negative

it will becomes this:
type=weaksubj len=1 word1=abandonment pos1=noun stemmed1=n priorpolarity=negative
type=weaksubj len=1 word1=abandonments pos1=noun stemmed1=n priorpolarity=negative
type=weaksubj len=1 word1=abandoned pos1=adj stemmed1=n priorpolarity=negative
type=weaksubj len=1 word1=abandon pos1=verb stemmed1=y priorpolarity=negative
type=weaksubj len=1 word1=abandons pos1=verb stemmed1=y priorpolarity=negative
type=weaksubj len=1 word1=abandoned pos1=verb stemmed1=y priorpolarity=negative
type=strongsubj len=1 word1=abase pos1=verb stemmed1=y priorpolarity=negative
type=strongsubj len=1 word1=abases pos1=verb stemmed1=y priorpolarity=negative
type=strongsubj len=1 word1=abased pos1=verb stemmed1=y priorpolarity=negative

Which is quite similar job but how to make it do the job one time, not repeat and make redundancy?
 
Old 02-10-2011, 09:35 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,012

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Yes just follow the process. You can either combine all changes to a 'verb' or place adding 's' in part and 'ed' in another.
 
Old 02-10-2011, 11:38 PM   #11
nobtiba
Member
 
Registered: Jan 2007
Posts: 40

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Yes just follow the process. You can either combine all changes to a 'verb' or place adding 's' in part and 'ed' in another.
I tried this:
awk -F"[ =]" '$8 == "noun"{line=$0;$6=$6"s";$0=line"\n"$0}1;$8 == "verb"{line=$0;$6=$6"s";$0=line"\n"$0}1{line=$0;$6=$6"ed";$0=line"\n"$0}1' file

but it works differently, not as expected... May be I understand the parameters wrongly ?
 
Old 02-11-2011, 12:18 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,012

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Your first issue is the '1' after each section. The '1' is there to tell awk to print (ie the default action for a true expression).
The second is that any pair of braces {} not proceeded by an expression (ie $8 == "verb") will run for every line in the file.
So I am guessing your example, apart from printing multiple entries due to first point, is adding 'ed' to every line irrelevant of being a 'verb'.
 
1 members found this post helpful.
Old 02-13-2011, 08:40 PM   #13
nobtiba
Member
 
Registered: Jan 2007
Posts: 40

Original Poster
Rep: Reputation: 0
Thanks Grail ! Great help !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to add blank lines whenever the value of a given column alters in a data file? vcmota Programming 9 01-11-2011 08:47 PM
Add comma to end of lines in text file Johng Programming 9 08-21-2010 04:15 AM
Add lines to a file isra Debian 5 03-05-2007 06:22 AM
how to add lines to a .conf file richeyc Linux - Hardware 2 04-29-2006 03:25 PM
How can I use a shell script to add and replace lines in a file? abefroman Programming 10 12-27-2005 05:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration