Thread: Append text

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Append text


    I'm using the below command but .txt is appended to the beginning of the file names not in the end. how to display it like filename.txt?

    for test in `cat files.lst`; do echo $test".txt"; done

    .txtrce
    .txt
    .txtge
    .txttion
    .txt
    .txtheist
    .txtave
    .txt_tion

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,394
    Rep Power
    1688
    What happens if you try:
    Code:
    for test in `cat files.lst`
    do
      echo "${test}.txt"
    done
    or
    Code:
    for test in `cat files.lst`
    do
      echo "${test}\c"
      echo ".txt""
    done
    or
    Code:
    for test in `cat files.lst`
    do
      x="${test}.txt"
      echo $x
    done
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0
    No output for the 2nd one. For the rest,

    .txtrce
    .txt
    .txtge
    .txttion
    .txt
    .txtheist
    .txtave
    .txt_tion

    Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,394
    Rep Power
    1688
    Ok, time to see the actual contents of that file ..!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,394
    Rep Power
    1688
    Having actually tried the code now (and having had to correct the error in the second one (removing the extraneous "), plus adding -e to the echo (needed as I am using bash under Linux to have the \c work)) they have all worked as planned. Indeed, as I suspected, your code also works, which makes me think that the source of the problem is your file. I suspect you have blank lines interspersed within it - add a pipe to your cat to remove them: (| grep -v "^$") and see how that goes:
    Code:
    for test in `cat files.lst | grep -v "^$"`; do echo $test".txt"; done
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo