How to get it in single line. pls help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get it in single line. pls help
# 1  
Old 08-05-2008
MySQL How to get it in single line. pls help

Hi,
when i am executing this script, i am getting the values of TID and Intime in different lines.
How can i print both output values in a single line. please advise

_____________________________
#!/bin/ksh
export Path="/abc/def/ghi";
Home="/abc/jkl/prt";
cd $Path
cat $Home | while read line
do
TID=$(echo $line|awk '{print $4}');
Intime=$(ls -ltr | grep $TID | awk '{print $8}')
print $line
print $Intime
done
______________
Output : Prateek
12:15
Desired outpt : Prateek 12:15
------------------
Thanks in Advance
# 2  
Old 08-05-2008
well, the option i know is use "echo" instead of print
and use the option "-n" to tell echo not to output a new line

replace

print $line
print $Intime

for

echo -n $line
echo $Intime

buuut, that script looks really, hmmm, is like using "cat file | grep something"

i strongly suggest you stop using awk in that manner
also, exporting a variable called HOME and one called PATH is not really smart ....
also, not cheking if the cd worked can be dangerous (i know a guy that used cd and then deleted all the files in that folder, but the cd didnt worked and he deleted the /bin folder)
|| means, "if the previos command failled, execute this", is the oposite of && that means "if the previous command ended ok do this"

feeding the while from below is faster and cleaner

awk can output adta, dont need to create a variable (and use memory and cpu cycles)
printf works pretty much liek the c analog, and if you want a new line, you have to tell awk to do so (in the second printf , the \n)

BEWARE, im doing this blindly in the forum, so check it out for simple syntax errors

if you need more info, read this,. is like my bible when it comes to scripting

Advanced Bash-Scripting Guide
i also noticed that you use ksh
any reason to do so?
any ways, that bash "manual" is still good :P


Code:
#!/bin/ksh
#some comment about what do the files and folder are
folder1="/abc/def/ghi";
file1="/abc/jkl/prt";
# always separate the var creaton with the "main"


cd $folder1 || echo "cat cd to folder: $folder1"; exit 1


while read line
do
      echo $line|awk '{printf "%s",$4 }'
      ls -ltr | grep $TID | awk '{printf "%s\n",$8}'

done < $file1

# 3  
Old 08-05-2008
Quote:
Originally Posted by Prat007
Code:
..
print $line
print $Intime
..

Print in the same line Smilie
Code:
print $line $Intime

# 4  
Old 08-06-2008
Concur with broli that the script can probably be refactored to make better use of awk.

Code:
#!/bin/sh

Home="/abc/jkl/prt";
Path="/abc/def/ghi"

ls -ltr "$Path" | awk 'NR==FNR { t[$4] = $4; next }
{ print $0, t[$4] }' - $Home

I'm not sure I really grasp what TID is supposed to be, so you might want to change t[$4] to something else. I assumed TID will be the fourth field in the file $Home. (Less weird variable names would help, too.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

2. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

3. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

4. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

5. Shell Programming and Scripting

Need Help with greping two line from a file Pls help ASAP

Hi all - I''m in a little bit of jam - If you can please help I have a file that has the following content ( please see below) I need to read the file and then get this result in this format ------------- To put out in this format name: sophis Total: 22328 name: tca ... (2 Replies)
Discussion started by: mnassiri
2 Replies

6. Shell Programming and Scripting

What's wrong with this command line pls?

I executed the following command and got Arg List too long. find /dir1/dir2/LOG_* -prune -type f -mtime +3 -exec echo {} \; What's that about? (2 Replies)
Discussion started by: bbbngowc
2 Replies

7. Shell Programming and Scripting

line separate (pls)

Hello everybody I made code to move some files to another place for example(I moved MM file TO BIN folder ) and the MM path I put it in different file e.g(0:HOME/unixstuff/MM) so my question is who I can separate the path line and use the number 0: and MM as parameter because if I want to... (2 Replies)
Discussion started by: falm
2 Replies

8. Shell Programming and Scripting

check position of end of line(URGENT PLS)

I Have to check in a file that all the lines are ending at same posiotin. Ex : line 1 is ending at position 88 line 2 should at same position i.e 88 Thanks in advance (6 Replies)
Discussion started by: evvander
6 Replies

9. UNIX for Dummies Questions & Answers

0403-057 Syntax error at line 70. pls help

Hi All, I got a script from one of the unix forums for reporting on filesystem usage and wanted to use it but it keeps giving me the following error. 0403-057 Syntax error at line 70 The script is shown below. Pls help as I am new to UNIX. # set -x # D I S K S P A C E . S H # #... (2 Replies)
Discussion started by: OMONI
2 Replies

10. UNIX for Dummies Questions & Answers

One line of cron is not working - PLS Help

Hi all, I have spent 2 hours going through the forum and have not found an answer to my question, I hope someone can help. I have cron setup to run two commands, one at 1am and one at 3 am, the one at 1 am works but not the one at 3 am. The time is set as: 0 1 * * * /path/to/file 0 3 * * *... (3 Replies)
Discussion started by: burnie
3 Replies
Login or Register to Ask a Question