Capture Multiple Lines Into Variable As Of Standard Output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Capture Multiple Lines Into Variable As Of Standard Output
# 1  
Old 05-31-2013
Capture Multiple Lines Into Variable As Of Standard Output

Hello All,
I have the below script and output.

Code:
cat test.sh
#!/bin/bash -x
logit()
{
 echo "[${USER}][`date`] - ${*}" > ${LOG_FILE}
}
LOG_FILE=/home/infrmtca/bin/findtest.log

VAR=`find . -type f -name "*sql"`
logit $VAR

Output:
Code:
 cat /home/infrmtca/bin/findtest.log
[infrmtca][Fri May 31 15:02:59 EDT 2013] - ./dwh_StageDataValidation.sql ./dwh_MoveDataFromStageToWarehouse.sql

Here as you the output of find command is printed in same line in log file. Instead i would like it to be printed as a list as of standard output when you execute the find command like below. please help.

Code:
find . -type f -name "*sql"
./dwh_StageDataValidation.sql
./dwh_MoveDataFromStageToWarehouse.sql

Expected Output:
Code:
[infrmtca][Fri May 31 15:02:59 EDT 2013] - 
dwh_StageDataValidation.sql dwh_MoveDataFromStageToWarehouse.sql

# 2  
Old 05-31-2013
Using GNU find:
Code:
echo -e "[${USER}][$(date)] -\n$(find . -type f -name "*sql" -printf "\t%f\n")"

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-31-2013
I find it amusing that you use the -printf option of find, yet feel the need to use echo -e to use \n, rather than use printf!
This User Gave Thanks to Scott For This Post:
# 4  
Old 05-31-2013
I modified my script as per your inputs but still not getting desired output.
Code:
#!/bin/bash -x
logit()
{
 echo "[${USER}][`date`] -\n${*}" > ${LOG_FILE}
}
LOG_FILE=/home/infrmtca/bin/findtest.log

VAR=`find . -type f -name "*sql" -printf "\t%f\n"`
logit $VAR

Output:
Code:
[infrmtca][Fri May 31 15:36:40 EDT 2013] -\ndwh_StageDataValidation.sql dwh_MoveDataFromStageToWarehouse.sql

# 5  
Old 05-31-2013
Quote:
Originally Posted by Ariean
Code:
VAR=`find . -type f -name "*sql"`
logit $VAR

You need to double quote $VAR when you call logit, otherwise the newlines are lost to word splitting.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 05-31-2013
@Scott, I thought of using printf instead of echo.

But using format specifiers with printf would make the code appear cryptic, hence used echo instead.
# 7  
Old 05-31-2013
The echo in logit needs an -e option in order to interpret the \n correctly.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

2. Shell Programming and Scripting

How to capture system() function output in variable

How to capture system() function output in awk variable and the print that awk variable..... (8 Replies)
Discussion started by: bharat1211
8 Replies

3. Programming

capture the output of printf into another variable

Hi , I wonder if in java I can pipe the below output of the printf into a variable: System.out.printf(" This is a test %s\n", myVariable); I want to keep the output of the printf command to create my history array. Thanks. (2 Replies)
Discussion started by: arizah
2 Replies

4. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

5. Shell Programming and Scripting

Logfile parsing with variable, multiple criterias among multiple lines

Hi all I've been working on a bash script parsing through debug/trace files and extracting all lines that relate to some search string. So far, it works pretty well. However, I am challenged by one requirement that is still open. What I want to do: 1) parse through a file and identify all... (3 Replies)
Discussion started by: reminder
3 Replies

6. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies

7. Shell Programming and Scripting

How do I capture multiple lines of the status output of a command?

I need to know what the upload speed of an Internet connection. I thought the easiest way to do this would be to transfer a file via FTP to my server using the command: sh-3.2$ ftp -u ftp://username:password@computerdomain/directory/ file_to_be_uploaded Note: My environment allows me to issue... (2 Replies)
Discussion started by: zzz1528
2 Replies

8. Shell Programming and Scripting

Standard output redirection from a variable

Hi, trying to store a comand involving a redirection in a variable and then run this variable. But the redirection gets lost. Ex: #!ksh MYCMD="ls -l > dirlist.txt" $MYCMD This runs the command but displays the result in the terminal instead of redirecting it to the text file. Can... (4 Replies)
Discussion started by: rm-r
4 Replies

9. Shell Programming and Scripting

Rewriting standard output lines

Hello I'm curious about how to get a bash script to rewrite a line of standard output. For example, many programs track their progress by writing percentages on the screen: Precent Done: 60% That line gets updated periodically to reflect the status.. My question, is how do we do this, as... (5 Replies)
Discussion started by: neked
5 Replies

10. Shell Programming and Scripting

[csh] How to capture output from a command and pass it on to a variable?

Hi there! I'm trying to write a script that will capture output from a command and assign it to a variable. Let's say, for example, I'd like to catch from inside the script whatever the following command outputs: ls *.aaa and put it into a variable "listoffiles". What I tried was: set... (3 Replies)
Discussion started by: machinogodzilla
3 Replies
Login or Register to Ask a Question