LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-15-2009, 09:21 AM   #1
CHARL0TTE
LQ Newbie
 
Registered: Jul 2009
Posts: 14

Rep: Reputation: 0
Text editing: Adding a digit/text to the end of a row


Hi,

I hope that you can help me.

I have a text document which is generated using a shell script that I have written; however, there is one bit that I am struggling with. I've looked at various commands, but can't seem to work this one out so hoping you can help. I actually have 2 questions, but they are both of the same nature.

I'll use an example to illustrate as I'm not very good at explaining...

EXAMPLE TEXT FILE:

AB1 13000 1 1
AC1 15678 1 2
DB1 18729 2 2
DB1 19000 1 1
AC1 24000 2 1

Using the above example, I would like to do 2 things. First, I would like to simply insert the digit 5 (separated by white space) to the end of each row. So, I'd like to end up with:

AB1 13000 1 1 5
AC1 15678 1 2 5
DB1 18729 2 2 5
DB1 19000 1 1 5
AC1 24000 2 1 5

On the same initial text file, but for a separate output, I would like to add together $3 and $4 and, if the result equals 3, insert the digit 0 to the end of the row. If adding $3 and $4 does not equal 3, I would like to insert the digit 1 to the end of the row. I figure it requires an IF/ELSE function, but Iím really not sure how to do it in this way. So, I'd like to end up with:

AB1 13000 1 1 1
AC1 15678 1 2 0
DB1 18729 2 2 1
DB1 19000 1 1 1
AC1 24000 2 1 0

I want to produce 2 separate outputs for these 2 tasks, so even if you can only help with one of my questions that would be excellent.

Many thanks,

Charlotte
 
Old 07-15-2009, 09:38 AM   #2
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,087

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
Charlotte,
All your posts look like "homework". Are they?
 
Old 07-15-2009, 09:42 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
This tutorial is very good:
http://www.grymoire.com/Unix/

Adding '5' at the end of each line is quite easy in SED.

Your second output can be achieved with AWK.

It will be well worth investing some time in learning those two commands.

Last edited by sycamorex; 07-15-2009 at 09:44 AM.
 
Old 07-15-2009, 09:48 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I have the same hunch about homework......

For the first question, look at SED. My favorite tuturial is here: http://www.grymoire.com/Unix/Sed.html Hint: adding text at the end of a line is trivial.

Second question:
AWK is good for extracting specific fields (tutorial at the same site as above). Basically, you will do these steps for each line:
extract two variables
add them
test
if the test matches, then use SED to add the desired text to the end of the line.

Regardless of the homework question, tell us more about what books or other references you are using.
 
Old 07-15-2009, 09:49 AM   #5
CHARL0TTE
LQ Newbie
 
Registered: Jul 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tredegar View Post
Charlotte,
All your posts look like "homework". Are they?

Hello Tredegar. No, they are not homework. I am a little too old for that! Basically, I am collecting some data, which is generated by another program. The program which generates the output files, however, creates a lot of useless information. Plus, I have to run my analyses based upon the data. So, I need to manipulate the output files from one program so that they are useful for another.
 
Old 07-15-2009, 09:57 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Quote:
Originally Posted by CHARL0TTE View Post
Hello Tredegar. No, they are not homework. I am a little too old for that! Basically, I am collecting some data, which is generated by another program. The program which generates the output files, however, creates a lot of useless information. Plus, I have to run my analyses based upon the data. So, I need to manipulate the output files from one program so that they are useful for another.
In that case, you'll definitely need to use awk/sed in the future, which is a very good reason to learn at least the basics of them.
The above mentioned tutorial is really good. If you don't have time to read it now - googling it will give you enough examples.
 
Old 07-15-2009, 10:30 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Sorry for the overlap with sycamorex (I'm really slow)

"Too old for homework"-----Huh??

Seriously, did you look at the SED tutorial we both recommended?
 
Old 07-15-2009, 10:39 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Hello CHARL0TTE

This does it within the shell (and assumes the output files are empty or do not exist)
Code:
while read
do
	fields=($REPLY)
	echo $REPLY 5 >> output1
	let i=${fields[2]}+${fields[3]}
	if [[ $i = 3 ]]; then
		echo $REPLY 0 >> output2
	else
		echo $REPLY 1 >> output2
	fi
done < input
Best

Charles
 
Old 07-15-2009, 10:47 AM   #9
CHARL0TTE
LQ Newbie
 
Registered: Jul 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you for your replies Sycamorex and Pixellany. I'll certainly have a look at the links you've suggested. I had already come across the sed command, but haven't quite got to grips with it (only really been playing around with this for a couple of days - I think this is more due to conceptual gaps at the moment!

I'm trying to teach myself with the aid of a Linux pocket guide and the internet. If I don't get to grips with these things I'll have to repeatedly do this sort of task manually with hundreds of data files, which seems silly. Having no scripting background though means rather slow progress at the moment.

Anyway, thank you for your advice and suggestions.
 
Old 07-15-2009, 10:52 AM   #10
CHARL0TTE
LQ Newbie
 
Registered: Jul 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Hello CHARL0TTE

This does it within the shell (and assumes the output files are empty or do not exist)
Code:
while read
do
	fields=($REPLY)
	echo $REPLY 5 >> output1
	let i=${fields[2]}+${fields[3]}
	if [[ $i = 3 ]]; then
		echo $REPLY 0 >> output2
	else
		echo $REPLY 1 >> output2
	fi
done < input
Best

Charles

Thank you for your help Catkin. I shall try adding it to my existing script and see what happens! Seems like I have a lot of learning to do!
 
Old 07-15-2009, 10:52 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by catkin View Post
Hello CHARL0TTE

This does it within the shell (and assumes the output files are empty or do not exist)
Code:
while read
do
	fields=($REPLY)
	echo $REPLY 5 >> output1
	let i=${fields[2]}+${fields[3]}
	if [[ $i = 3 ]]; then
		echo $REPLY 0 >> output2
	else
		echo $REPLY 1 >> output2
	fi
done < input
Best

Charles
Doesn't this append to files, as opposed to appending to individual lines?
 
Old 07-15-2009, 06:49 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
As you say you have a minimal scripting background, here's some very good links:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Welcome to LQ

Good Luck
 
Old 07-16-2009, 03:20 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by pixellany View Post
Doesn't this append to files, as opposed to appending to individual lines?
Yes. File output1 has the contents of the original file with " 5" appended to each line. File output2 has the contents of the original file with ' [01]' appended to each line.

You 'ave me bang to rights, guv -- that's not what CHARL0TTE asked for -- but I figure moving output1 to input is trivial and there's a lot to be said for leaving the input file untouched until the program is tested.
 
Old 07-16-2009, 06:44 AM   #14
CHARL0TTE
LQ Newbie
 
Registered: Jul 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
As you say you have a minimal scripting background, here's some very good links:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Welcome to LQ

Good Luck
Hi Chrism01,

just wanted to say thank you for the sites you have suggested. I will certainly take a look at them. Everyone here is very kind!

Best wishes

Charlotte
 
  


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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
Paste each single row from n separate text files into a new file containing n rows Mike_V Programming 11 04-27-2009 11:51 PM
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 10:40 AM
[C++] append to each row of a text files sylvaticus Programming 2 10-18-2007 07:22 AM
Delete the first row from a text file loopoo Linux - Newbie 2 08-15-2006 02:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:43 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration