Results 1 to 7 of 7
hello everyone, I've been given an assignment from school. My teacher has given me a simple text file that looks as followed.
________________
Asda Cola
asda Fanta
Adsa Beer
Asda ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 01-14-2013 #1Just Joined!
- Join Date
- Jan 2013
- Posts
- 1
Regular expressions, Text editing in the command line
hello everyone, I've been given an assignment from school. My teacher has given me a simple text file that looks as followed.
________________
Asda Cola
asda Fanta
Adsa Beer
Asda Wine
________________
* note there are some spelling errors in this list.
I have to make a script that turns the above list into something like this.
_______________
Asda Cola
Fanta
Beer
Wine
________________
I can get it to work without the spelling errors in the first list using these simple commands.
Sed 's/Asda//g' Asda.txt > Asdafin.txt
clear
sed -i '1s/^/Asda /' Asdafin.txt
clear
This however isn't quite the way it should be because if I accidentally misspell Asda it won't copy that line.
I know there is something like regular expressions but I'm not quite sure how to implement this in my script.
Any tips and pointers are really appreciated.
Thanks in advance, Mikhail.
- 01-14-2013 #2Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
hi,
is 'Adsa Beer' intentional?
otherwise:Code:sed '2,$s/[aA]sda *//' Asda.txt
- 01-14-2013 #3
See #3 in the Forum Rules
Homework is there to learn. Do that - it won't do any harm and you are on the right track with regular expressions.
Last edited by Kloschüssel; 01-14-2013 at 01:15 PM.
- 01-16-2013 #4
I don't think it's fair to attack him for this post, man. He came up front about it being homework, he put forth an honest effort, and was just asking for a push in the right direction after he showed the work he has thus far. He's not like what that rule was aimed at which is the people trying to slip their homework under the radar the night before it's due without doing any work on it or even attempting it.
In another words, chill out...
- 01-16-2013 #5
How about this one ?
Asda.txt will haveCode:head -1 Asda.txt > final tail -n +2 test1 | awk '{print $2}' >> Asdafin.txt
________________
Asda Cola
asda Fanta
Adsa Beer
Asda Wine
________________
and Asdafin.txt will have
Asda Cola
Fanta
Beer
Wine
________________
Jazak Allah
Sohail
- 01-16-2013 #6Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
reread, and rewrite your code: filenames don't match.
Originally Posted by msohail
the more external command a script includes, slower it is.
cut should be prefered to using awk for simply cutting a field on an obvious separator
this way I don't care if it's adsa or asda.Code:sed '2,$s/[^ ]* //' yourFile
- 01-16-2013 #7


Reply With Quote

