i have a problem when I want to output several variables to txt file in shell. I want them in one line, but there appear line break after first and third variable. I have for example this
for s in `ls $TESTDIR/results_orig.txt`
do
t_a=(`grep " a" $s`);
t_b=(`grep "Bytes written to file: " $s`);
t_c=(`grep " Total Time:" $s`);
t_d=(`grep "QP " $s`);
a=${t_a[2]};
b=${t_b[4]};
c=${t_c[3]};
d=${t_d[2]};
echo "$a $b $c $d" >> $TESTDIR/results.txt;
done
I got the variables t_a to t_d are after I parse original txt file to find some values in it. Than I want to find some number ant write them in file without other text that I have in original file. And I want it to write those parsed numbers in one line in new file, like
13.2 1678 3231.5 2422.1
And I get
13.2
1678 3231.5
2422.1
When I echo any variable separately to the console I get right values. So variables are parsed fine. What could be wrong?
Thank you.
Related
csv file with the first row has header file. I need to read this first row and then parse it to see if it has the elements i'm looking for.
First row has 4 elements. 1. HDR 2. today's date 3. From date 4. To date.
here is the code i used to get the first row.
read -r header < "1" -- this game me the first row into header variable.
I tried to read this 'header' variable to further split the row.
read f1 f2 f3 < “$header”
echo "OS is : $f1"
echo "Company is: $f2"
echo "Value is : $f3"
i'm getting no values displayed. I think the reason could be 'header' is not coming in as a string.
I'm new to unix shell scripting. Please help.
read -r header < "1"
No. You are reading from a file named 1 (which probably does not exist, so read is failing) because < is used for redirection. Try typing that very command in a terminal, you'll get an error ("no such file or directory").
read f1 f2 f3 < “$header”
This is wrong. If the header shell variable contains 1 2 3 you are reading from the file named 1 2 3 (a six character file path with two spaces inside) because < is used for redirection
Consider using awk to process files made of lines containing several columns.
Consider starting your (executable) shell script with #!/bin/bash -vx (to get traces) during the debugging phase. Spend a few hours reading some shell scripting tutorial and then the bash reference manual.
Be sure to understand how shells work, notably their globbing. Be aware that every program (e.g. started by some shell) is started by execve(2) (done in some process, e.g. your shell process).
BTW, before the read f1 f2 f3 < “$header” you might (temporarily) add debugging outputs, e.g.
echo "header=" "$header" > /dev/stderr
in your shell script, to understand what is going on.
(You really should spend days in reading more about shells)
I am trying to create a parameter file in shell script. And when i am trying to write variables into the file it is writing it's content in different line.
Input variables
var1="1,2,3,4"
var2="100,200,200,400"
var3="10,11,12,13"
How i am writing these variables into file
echo "VAR1="$var1 >> file.param
echo "VAR2="$var2 >> file.param
echo "VAR3="$var3 >> file.param
Expected file content
VAR1=1,2,3,4
VAR2=100,200,200,400
VAR3=10,11,12,13
But for VAR1 numbers are written in a line and for VAR2 and VAR3 numbers are not written as expected.
VAR1=1,2,3,4
VAR2=100
,200
,200
,400
VAR3=10
,11
,12
,13
Can anyone please help me why this is happening and how can i prevent it.
this is my first stackoverflow question, regarding bash scripting. I am a beginner in this language, so be kind with me.
I am trying to write a comparison script. I tried to store all the outputs into variables, but only the last one is stored.
Example code:
me:1234567
you:2345678
us:3456789
My code:
#!bin/bash
while read -r forName forNumber
do
aName="$forName"
echo "$aName"
aNumber="$forNumber"
echo "$aNumber"
done < "exampleCodeFile.txt"
echo "$aNumber"
For the first time, everything will be printed out fine. However, the second echo will only print out "3456789", but not all the numbers again. Same with $aName. This is a problem because i have another file, which i stored a bunch of numbers to compare $aNumber with, using the same method listed above, called $aMatcher, consisting:
aMatcher:
1234567
2345678
3456789
So if i tried to run a comparison:
if [ "$aNumber" == "$aMatcher" ]; then
echo "match found!"
fi
Expected output (with bash -x "scriptname"):
'['1234567 == 1234567']'
echo "match found!"
Actual output (with bash -x "scriptname"):
'['3456789 == 3456789']'
echo "match found!"
Of course my end product would wish to list out all the matches, but i wish to solve my current issue before attempting anything else. Thanks!
When you run your following code
aNumber="$forNumber"
You are over-writing the variable $aNumber for every line of the file exampleCodeFile.txt rather than appending.
If you really want the values to be appended, change the above line to
aNumber="$aNumber $forNumber"
And while matching with $aMatcher, you again have to use a for/while loop to iterate through every value in $aNumber and $aMatcher.
I have a very basic script that starts with saving part of input argument to a varialbe:
dirN= basename $1
echo $dirN
$dirN was displayed as expected
then I try to cat a string with $dirN
tmp="/some/path/$dirN"
when I echo $tmp, it only displays /some/path/
I tried overwriting tmp
tmp=$dirN
and echo $tmp,
it shows nothing.
it's like $dirN was never stored, but it was echoed from line 2
I am very confused, so was my colleague.
Any hint?
Thank you all!
I believe you want
dirN=`basename $1`
to actually store the value returned instead of
dirN= basename $1
The "echo $dirN" is not showing anything in your version, it's the previous line that shows the output you attribute to the echo.
I want to use echo to add data into a text file progressively. I wrote a small batch code as follows:
#echo off
echo >text.txt
set DllCopier_d=./DllCopier
echo %DATE:~04%>text.txt
echo %TIME:~0,5%>text.txt
echo %~dp0%>text.txt
When I look at text.txt at the end, i found only one line:
C:\omsstest_automation\win32\
Which is result of last line.
Why is the "echo" resulting into replacing previous contents of text.txt?
> means create a new file with these contents (replace the old if it exists), >> means append or create a new file if none already exists.
So to spell out the answer to your question, replace subsequent usages of > after the very first one with >>.