Find the answer to your Linux question:
Results 1 to 5 of 5
Hi! I have a problem that seemes to be quite easy but I could not solve. How can I put the result of the following expression in a variable (that ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    5

    Variable problem with sed

    Hi!

    I have a problem that seemes to be quite easy but I could not solve.

    How can I put the result of the following expression in a variable (that will be a file name in any case) in a c shell script?

    sed -n "$i{p;q;}" outputs.txt

    I could write it on the screen or in an another file but I would need it in a variable.

    Thanks in advance

  2. #2
    Just Joined!
    Join Date
    Jan 2008
    Posts
    17
    use parentheses, which must be escaped, to use variables. Then, put them into your result using escaped numbers.

    for example, to convert all .com domains in a stream to .net, you might use
    "s/\(.*\).com/\1.net/"

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    11
    Hi atterl,
    Try one of the following:
    Code:
    a=$(sed -n "$i{p;q;}" outputs.txt)
    a=`sed -n "$i{p;q;}" outputs.txt`
    Santiago

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    ... in a c shell script
    Along with the standard advice to avoid csh for scripting if at all possible, you can use part of the suggestion from chebarbudo to solve your problem:
    Code:
    set a = `sed -n "$i{p;q;}" outputs.txt`
    The quotes are the special backquotes [`] often found on the same key as the tilde [~].

    I use tcsh for interactive work, but the Bourne shell family (sh, bash, ksh, zsh) for scripting. For reasons, see Csh Programming Considered Harmful

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Just Joined!
    Join Date
    Jun 2010
    Posts
    5
    Thank you for your help.

    I tried to use parentheses and ` too, but I get the expression in the variable with or without the "s and not the result. I also tried to redirect the result into $a with a > or a | but I got an empty variable or an error. The expression a=$(sed -n "$i{p;q;}" outputs.txt) also gives an error both with and without set.

    I gave up using sed and c shell, although the latter one would have been better for the problem because the files I must hadle are genereted by a software running in c shell.

    Anyway I solved the problem in b shell as follows:

    Code:
    #!/bin/bash
    
    while read line
    do
      echo $line
      ...
      other scripts for data manipulation I needed
      ...
    done < outputs.txt

Posting Permissions

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