Find the answer to your Linux question:
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.
  1. #1
    Just 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.

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    211
    hi,

    is 'Adsa Beer' intentional?
    otherwise:
    Code:
    sed '2,$s/[aA]sda *//' Asda.txt

  3. #3
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    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.

  4. #4
    Linux Newbie Syndacate's Avatar
    Join Date
    May 2012
    Location
    Hell..no literally, this state is hell..
    Posts
    192
    Quote Originally Posted by Kloschüssel View Post
    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.
    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...

  5. #5
    Just Joined! msohail's Avatar
    Join Date
    Nov 2011
    Posts
    47
    Quote Originally Posted by mikhail90 View Post
    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.

    How about this one ?

    Code:
    head -1 Asda.txt  > final
    tail -n +2 test1 | awk '{print $2}' >> Asdafin.txt
    Asda.txt will have
    ________________
    Asda Cola
    asda Fanta
    Adsa Beer
    Asda Wine
    ________________

    and Asdafin.txt will have

    Asda Cola
    Fanta
    Beer
    Wine
    ________________

    Jazak Allah
    Sohail

  6. #6
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    211
    Quote Originally Posted by msohail
    How about this one ?
    reread, and rewrite your code: filenames don't match.

    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

    Code:
    sed '2,$s/[^ ]* //' yourFile
    this way I don't care if it's adsa or asda.

  7. #7
    Just Joined! msohail's Avatar
    Join Date
    Nov 2011
    Posts
    47
    Quote Originally Posted by watael View Post
    reread, and rewrite your code: filenames don't match.

    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

    Code:
    sed '2,$s/[^ ]* //' yourFile
    this way I don't care if it's adsa or asda.

    I agree

    Jazak Allah
    Sohail

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •