Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0

    Writing input data in file


    Hi guys,

    i want to write an input data to right in | delimited data. i tried below one

    for i in `cat file1.txt`
    do
    echo $i|awk -F"|" '{$2="Value";print $0}'>>newfile.txt
    done


    file1. text

    hello|input|xx|yy|zz

    but i got the output as
    hello Value xx
    yy value
    ...


    i needed
    hello|Value|xx|yy|zz


    can any one help
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0
    try this:

    for i in `cat file1.txt`
    do

    echo $i|awk '{$2="Value";print $0}'>>newfile.txt

    done

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    hi buddy
    throwing syntax error...

    syntax error near unexpected token `('
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    You don't need to "for i in cat foo.txt" with awk -- it is going to check every line ("record", in the view of awk) in the file/stream you pass to it.
    bash Code:
    [ceverett@taco n00b]$ cat << EOF >> foo.txt
    > aa|bb|cc|dd
    > ee|ff|gg|hh
    > EOF
    [ceverett@taco n00b]$ awk 'BEGIN { FS = "|" } {print "Value:"; {for(i=1;i<=NF;i++) {print ($i)}}}' foo.txt
    Value:
    aa
    bb
    cc
    dd
    Value:
    ee
    ff
    gg
    hh

    This is probably not the output you are looking for. But I don't care, because you didn't give an example or an explanation of what you are trying to do. You should be able to work the pattern above into whatever you need and finish your homework. But really, rtfm is the best advice for simple stuff like this -- awk has a bajillion convenience functions already built in, all you have to do is search the docs to find them. awk is really useful, by the way... its a much cleaner language than bash (but written for a very different purpose).
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    Thanks buddy,

    i will try this and wat i needed is :


    file1.txt output file:
    xx|yyy|zzz|"input which i will give"
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,446
    Rep Power
    1751
    Your requirement/specification has changed a little from the original post!
    To help us to help you (it will also help you think about the problem) can you let us know:
    Is the input file fixed in format (always the same number of | delimited fields?)
    Is the input file comprised of one line or multiple lines?
    Do you want this 'transformation' to happen to every line in the input file?
    Will the new text always be in quotes?
    Is it always in the same place you wish the new text to go, and will it over-write/replace an existing field or be inserted?
    Can you show an example of the input file (with just a few lines if it is multiple lines) and a matching output file of what you'd want to see.
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    Guys let me explain my requirement clearly now.
    I am having an input file for eg. file.txt which contains pipe delimited rows

    file.txt:
    xx|yyy|zz|12|3|aaaaa|.....
    yy|zz|1|3|4|xxxxx|.......
    .
    .
    .

    i want the above file name to be transformed into another file with giving input from the user and replacing the corresponding fields based upon the input. Below is the eg:
    xx|yyy|"input"|for number generate random numbers|...
    yy|"input"|1|random number|4|xxxxxx|.....
    .
    .
    .
    so finally my redirect file would have:
    output_file.txt:
    xx|yyy|input|random number|3|aaaaa|.....
    yy|input|1|random number|4|xxxxx|.......
    .
    .
    like wise

    hope this would have helped you guys for better understandin
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,446
    Rep Power
    1751
    Let's see if we can get this clear.
    You start with an input file:
    Code:
    xx|yyy|zz|12|3|aaaaa|.....
    yy|zz|1|3|4|xxxxx|.......
    I am guessing the "...." at the end of each row suggest there are far more fields than those shown.

    Now, we seem to be replacing the filed in each row that has the value of zz with another value - presumably supplied earlier by the user.

    The bit I do not understand is why there is suddenly 'random number' in the output file - do you mean that that field (which from the example is always field 4 as there seems to be no other way of identifying it) is to end up with a random number in it? If so, random from what range?
    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
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    random number here refers to in any of the column where number exists should be replaced with any random number between 1.100
    eg:
    xx|yy|1|3

    instead of 3 below:

    xx|yy|1|2(this is the random number which will be generated)
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,446
    Rep Power
    1751
    Use something like:

    Code:
    awk -F \| -V repl="$var"
      'BEGIN {OFS ="|"}
       {
         $3=sprintf("%d",rand()*100+1);
         $4=repl;
         print;
       }'  input.txt > output.txt
    Where $var is a variable containing whatever replacement text the user has entered and $3 and $4 are fields within awk I selected 'at random' to demonstrate and should be changed to the correct field for the row in question.
    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
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    Hi with this same concept which you told above can i loop it to print multiple times

    for eg
    input file i am around 3 rows and can it be printed N number of times with replacing and generating random number and printing in output file
    hence output file will contain: 3*N rows
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,446
    Rep Power
    1751
    Yes, it can ... depending on exactly what you want to do:

    Code:
    awk -F \| -V repl="$var"
      'BEGIN {OFS ="|"}
       {
         $4=repl;
         for (i=0;i<3;i++) {
           $3=sprintf("%d",rand()*100+1);
           print;
         }
       }'  input.txt > output.txt
    Not tested so may need tweaking!
    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
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    buddy tried the above one its working but problem is its going infinte loop if i give input

    n=$2

    for (i=0;i<$n;i++)
    ..
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,446
    Rep Power
    1751
    Is the second field ($2) always numeric?
    In the for loop construct do NOT use $n just n:
    Code:
    n=$2
    
    for (i=0;i<n;i++) {
     ...
    }
    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
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    yes n will be always numeric which will be the input given by me at the time of execution of the script. so that it will run for n number of times and print the output
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo