0

I am new to Unix scripting and I am trying to write a script which should be able to append new line character and print the values inside a variable into new line,instead of same line.

Since data is dynamic and it may contain many more string rather only one,I have to add a loop and some conditions inside it to save the values in variable in new line.

Strings to be stored in variable in new line format:

"CA 1938" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" "CA 1937" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" "CA 1934" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

My thinking was to write some regex comparison and use that to run the insertion of new line in this varaiable.Below is the code with which I want to achieve that operation.

var="CA 1938 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX CA 1937 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX CA 1934 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
for i in $var; do
    #echo $i
   p="$p"$'\n'"$i"
done
echo "$p"

Expected output is

CA 1938 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA 1937 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA 1934 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Actual output is

CA
1938
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA
1937
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA
1934
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

So above actual output is coming because $i is all the string values which are separated by whitespace. Could you please help me in getting what I am looking for. Actually this CA(number of the task) and XX(task description).

1
  • I think your example is broken. You define var using backticks then never use it. In addition, p and i are never defined. I also doubt just do ... done was intentional without a loop. Commented Jul 8, 2015 at 14:50

3 Answers 3

2

This should work:

var='CA 1938 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX CA 1937 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX CA 1934 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
var="${var// CA/$'\n'CA}"
echo "$var"

Output:

CA 1938 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA 1937 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA 1934 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Sign up to request clarification or add additional context in comments.

1 Comment

You hit CA instead of other expression I like it bro thanks.
0

Using grep -o you can do this:

var='CA 1938 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX CA 1937 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX CA 1934 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

grep -oE 'CA\s+\S+\s+\S+' <<< "$var"
CA 1938 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA 1937 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA 1934 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

3 Comments

Thanks great help.Could you please explain a little about CA\s+\S+\s+\S+.
The grep will print each matching string on it's own line. It's looking for the text CA, followed by one or more spaces \s+, one or more non-spaces \S+ (your 1938, 1937, etc), one or more spaces, then one or more non-spaces (your XXXXXX...).
@Mr.Llama: Thanks so much for your comment. Yes indeed that is what the regex is doing.
0

You are not parsing the input correctly, for will break var into whitespace-separated parts (based on IFS) so you are actually adding a new line for each "word" in var, not each token.

As you suggested, I would use regular expressions with sed -r to properly parse it, and save it to a variable:

p=$(echo $var | sed -r 's/(CA \S+ \S+ )/\1\n/g')

\S matches any non-whitespace character, + matches "one or more character", so the regular expression is breaking the input into lines at every "CA" followed by two words.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.