Find the answer to your Linux question:
Page 1 of 4 1 2 3 4 LastLast
Results 1 to 10 of 34
I am trying to write a shell script that will take two files and rearrange the names and data below that name to be the same in both files. For ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Posts
    1

    NEED HELP- SHELL SCRIPT PLEASE


    I am trying to write a shell script that will take two files and rearrange the names and data below that name to be the same in both files. For instance in one file I have
    >ABC
    ATGAGGGDG
    >DEF
    GTACGGATT

    And in the other file I have
    >DEF
    GTACGGATT
    >ABC
    ATGAGGGDG

    Now I want the both files to be in the same order
    Can anyone please help me.
    Thanks very much.

  2. #2
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    Quote Originally Posted by http://www.linuxforums.org/rules.php
    Dont give us YOUR homework questions - Dont be so bloody lazy. If you post your homework for someone else todo, dont be supprised or get upset if your post is removed. Asking for a brief pointer on a *particular area* IS ok. Verbatim Copying of multiple assignment questions and attaching a note at the bottom saying "please help me" is not. We were all at school/college/uni once upon a time, and we know what homework questions look like

  3. #3
    Just Joined!
    Join Date
    Oct 2005
    Posts
    23
    I just thought that rather than creating an entirely new thread for this, I would use an EXISTING one.


    I'm having major difficulties with what I think must be something very simple to do.

    I've written a little script whereby a file is created that contains a list of all files present in the current directory. I now though would like to make it so that the number of files in the current directory is displayed on screen.

    Could anybody tell me how this is done? I'd be extremely grateful.
    It'd just be a simple 1-liner I would have thought, but I don't seem to be able to find it anywhere.. I know I'll need the "echo" command there first, but it;s the actual finding of the number of files that I don't quite know how to do.

    Thanks

  4. #4
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    I must be feeling kind today....since you shouldn't be posting your homework...

    Look into the wc -l command as well has how to pipe things, and then post what you come up with, if your having problems with a small part of that we might help you....
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

  5. #5
    Just Joined!
    Join Date
    Oct 2005
    Posts
    23
    This is MIGHTY confusing..

    So far I've written:

    #!/bin/sh
    -ls >> Files

    I think I understand that what that does. It lists the files present in the current directory and moves them into a file called "Files".

    I thought that I would need to follow that up with something like:

    echo -ls

    Which would actually DISPLAY the list itself...

    Is that illogical?


    I've read over pipes and see that they're a means of moving data from one place to another, but I don't much see how they can help me..
    The only moving of data I'm really doing is moving the list of files into "Files", but that can be done with ">>", can't it?



    .

    ..this'll be the death of me

  6. #6
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    I'm bored so I'll give you some more help.

    Okay with
    Code:
    #!/bin/sh
    
    ls >> Files
    will redirect the output from the redirect command into a file named File.

    now you want to know how many files there are.

    Firstly look up the usage of the wc command with
    Code:
    man wc
    . Look at about the third option down.

    Okay so you have a file called File, what you need to do is "read" this file and then redirect the command (pipe it) so it goes through the wc command and it should give you the number of files if you have used the right options and commands.
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

  7. #7
    Just Joined!
    Join Date
    Oct 2005
    Posts
    23
    Oh okay great thanks, I see. So I'm on track so far with the code I'd already written.

    I cannot check anything through Linux though as you suggested because I don't actually have access to Linux at all as I do this..

    I'm effectively working blind, which is I think perhaps what's making it slightly less easy to get my head around..


    Apologies for being a nuisance here..


    I think i better take a look on google to see exactly what this "wc" command is.

  8. #8
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    You can check the manpages online, this is the one for wc http://www.linuxcommand.org/man_pages/wc1.html

    btw. you realy should at least install something link cygwin if you want to do shell scripting on a none unix environment.
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

  9. #9
    Just Joined!
    Join Date
    Oct 2005
    Posts
    23
    Yeah, you're probably right about that. I'm on a shared computer though
    until next week, so i'm pretty stuck for now.


    I think I understand now though.

    So the wc command is basically to do with counting characters, lines, etc...


    Would that mean that this would work? :

    #!/bin/sh
    -ls >> Files
    echo "-l"

    ?


    It would be echoing the number of new lines in Files..

    Hm I'm assuming that when you use the -ls command to list files, they list them like this:

    file1
    file2
    file3
    file4
    file5

    Or else this wont work at all :shock:


    If when you list things they just list them like this:

    File1 File2 File3 File4, there'll only be ONE new line, so it wouldn't work.

    Maybe if that's the case I should use the -w command to count the number of WORDS..


    Blimey this is tricky.

  10. #10
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    Quote Originally Posted by Scarfinger
    I think I understand now though.
    So the wc command is basically to do with counting characters, lines, etc...

    Would that mean that this would work? :

    Code:
    #!/bin/sh
    ls >> Files
    echo "-l"
    ?
    It would be echoing the number of new lines in Files..
    Hm I'm assuming that when you use the -ls command to list files, they list them like this:

    file1
    file2
    file3
    file4
    file5
    Yes if you redirect from the ls command you'll get them in a nice line like above.

    Code:
    #!/bin/sh
    ls >> Files
    echo "-l"
    Okay this is better, but firstly where is the wc -l command? You'll have to use this, as you have worked out to count the number of lines.

    So you'll need to count the nember of lines in File <<-- What line of code woud you use for this?

    Secondly will File have a lest of each ls added to each other or will it just have the last ls in it? If it's the former the counting of the files becomes more difficult, while if it's the second you'll have to use > not >> to redirect.
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

Page 1 of 4 1 2 3 4 LastLast

Posting Permissions

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