4

I'm using ssh to connect to a remote machine and read a log file there. From that log file, based on some tokens, I extract specific logs and store it in a variable. Every log is in new line in the log file and the data can contain any character including white space.

array=("$(egrep "UserComments/propagateBundle-2013-10-19--04:42:13|UserComments/propagateBundle-2013-10-19--04:38:36|UserComments/propagateBundle-2013-10-19--04:34:24" <path>/propagateBundle.log)")
echo ${array[0]}
echo "$array"

First echo prints complete output in one line separated by white space while the other prints outputs in new line. Problem, is, I'm not able to save this output as an array. I tried this:

newArray=("$array")
max=${#newArray[@]}
echo $max

But echoing 'max' yields '1' on the screen. How can I save the output in an array? I also tried using

IFS=\`\n`

but could not get the data in an array.

EDIT

I used the solution given by Anubhav and it worked like charm. Now I faced a second issue. Since my data contains white spaces, so the array broke at white spaces and wrongly contained the one comments as multiple arrays. So, I used

IFS=\`\n`

and also used a $ symbol before backticks. Although this solves my problem, I still get an exception in the logs:

test.sh: line 11: n: command not found

Any suggestions?

2
  • 1
    You're using backquotes, which is the old-style synonym for $(...). The easiest way to do this is IFS=$'\n' (single quotes preceded by a dollar sign). Commented Oct 21, 2013 at 18:17
  • @chepner Thanks a ton friend!!!! That indeed worked!!! So many thanks.... Commented Oct 21, 2013 at 18:19

2 Answers 2

2

Don't put quotes in the command substitution:

array=( $(egrep "UserComments/propagateBundle-2013-10-19--04:42:13|UserComments/propagateBundle-2013-10-19--04:38:36|UserComments/propagateBundle-2013-10-19--04:34:24" <path>/propagateBundle.log) )

With quotes as in your code whole output is treated as single string in the array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Anubhava for the great response. Please see my Edited post. The second part is causing a problem now.
0

I've used IFS=('\n') otherwise all "n" chars disappears from results and sort command doesn't work properly. See bellow, it is a customized llq output.

#!/bin/bash
IFS=('\n')
raw=(`llq -f %id %o %gu %gl %st %BS %c`)
echo
echo ${raw[*]} | grep "step(s)"
echo
echo ${raw[*]} | grep "Step"
echo ${raw[*]} | grep "\---*"
echo ${raw[*]} | grep "bgp-fn*" | sort -k5 -r
echo ${raw[*]} | grep "\---*"
echo ${raw[*]} | grep "Step"
echo
echo ${raw[*]} | grep "step(s)"
echo

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.