Text editing: Adding a digit/text to the end of a row
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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:
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:
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.