Variable position in echo is changing the result - bash

I used grep to fetch result from two files and store the result in two variable respectively.
while I echo the output of two variable at that time if I change the position of variable then the result is changing .
The scenario is explained below with the example
i=123
res1=`grep S$i $NamesFile`
res2=`grep S$i $FeeInformation|awk '{$1="" ; print $0}'`
echo "$res1 $res2" ## this prints the value from both variable separated by space
echo "$res2 $res1" ## in this the variable two override the output of variable one
the issue is happening only on my terminal I tested it in other machine it is working fine , even In my terminal it was working till yesterday , don't know what change has happened which impacted this
NameFile contains the information like Student_id Name_of_student Mobile_No Location Email_id
FeeInformation file contains values Student_id Class Fee Admission_type(DOnation/Free)
Please suggest what could be the possible reason for this .
Thanks in advance

the linefeed character \r was causing the issue

Related

I need to assign a value following a specific string from a file into a variable in bash

Hope you can help....
Bash related question...
I have a folder with 3 files in it
654321_static1.txt
123456_static2.txt
321654_static3.txt
I need to find a value in 123456_static2.txt (note the random numbers preceding the _static2.txt are random and will change and will be unknown to me but the directpry path is constant.)
inside this file -> 12345_static2.txt will be many lines of text but there will always be this string
serverUuid="34543n54353453ewrwer" - I need to assign the value within the quotes to a variable.
var=34543n54353453ewrwer
Also note if this makes a difference that serverUuid="34543n54353453ewrwer" may appear once or many times but will always be the same value
Appreciate any help you can offer..
i think this should work
variable=$(grep serverUuid (filename) | cut -d '"' -f 2)

Delete set of lines from a file in AIX by passing variables

I am trying to delete a set of lines from a file by passing variables.
Below is my file :
$ cat checking.txt
Starting1
DELETE /*+NESTED_TABLE_SET_REFS+*/ FROM tables1
Ending1
Starting2
update table
set col1=2
where val2=685
Ending2
Starting3
update table
set col1=1
where val1=44
Ending3
so in above files I need to delete lines from 1st line to 4th line.
I used below command and it was working fine.
sed '1,4d' checking.txt
Now I gave variable a a value, like a=4
echo $a
4
Now I tried the sed command like
sed "1,${a}d" checking.txt
sed: 0602-404 Function 1, 4d cannot be parsed.
Can someone please tell me how to pass variable here?
Thanks in Advance
One way you could attempt this problem is in the following way. Assuming you want to delete lines between start and stop, you could write
awk '(NR<start) || (stop<NR)' start=1 stop=4 file
The way you are proposing it to work also works,
start=1
stop=4
sed "${start},${stop}d" file
The reason why it failed in your case is because the variable a seems to have blanks in front of it. You notice this from the errormessage and the blanks space in front of the 4.

Processing of the file names

I have a folder with a files inside with a long names like:
08_29_2017.AT1_dry_apo.3rep.step7_1.xtc
each file begins with a current data in a format like
dd_mm_yyyy.
My bash script loops all the files and defines only the relevant part of the file name in a new variable:
for traj in ${all_xtc}/*.xtc; do
traj_name3=$(basename "$traj")
traj_name="${traj_name3/.xtc/}" # here I remove xtc from the name
# here I need to add something to remove the date from the begining of the file
what should be passed here to remove a date from the beginning of a file name?
Thank you!
You can do what you want in two parameter expansions:
for traj in "$all_xtc"/*.xtc; do
traj=${traj##*/[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]}
traj=${traj%.xtc}
echo "$traj"
done
The first one removes everything up to the last slash (equivalent to basename), followed by the date. The second one removes the .xtc from the end.
The first expansion isn't at all strict about the date, since it would also match 98_76_0000, but it's probably good enough in this case. You can add a . to the end if you want to remove that as well.

AIX script for file information

I have a file, in AIX server, with multiple record entries in below format
Name(ABC XYZ) Gender(Male)
AGE(26) BDay(1990-12-09)
My problem is I want to extract the name and the b'day from the file for all the records. I am trying to list it like below:
ABC XYZ 1990-12-09
Can someone please help me with the scripting
Something like this maybe:
awk -F"[()]" '/Name/ && /Gender/{name=$2} /BDay/{print name,$4}' file.txt
That says... "treat opening and closing parentheses as field separators. If you see a line go by that contains Name and Gender, save the second field in the variable name. If you see a line go by that contains the word Bday, print out the last name you saw and also the fourth field on the current line."

bash script getting the value from a variable of the form var$[id]

This is the first time Im using a shell script ( #!/bin/sh ) and Ive been working my way through it reading tutorials and the like but Im stuck on this reading and writing values of a key..
Im trying to read in key=value pairs from a config file of the form
key1_begin=abc
key1_end=def
key2_begin=123
key2_end=jkl
.. and so on
I would like the user to pass in parameters to the script like
something.sh 1 x y z
where the first parameter would serve as an that is used to modify the appropriate keys. So after I have checked that the directory exists and the file exists I source it using
source config.cfg
I then save the id using ID=$1 and access the keys using
echo key${ID}_begin
so a read to obtain the value of the key would be
echo key${ID}_begin = $[key${ID}_begin]
where I expect to get " key1_begin = abc " but instead keep getting " key1_begin = 0 ". The same command however seems to work work for numbers. For example using this command with an ID of 2 gives " key2_begin = 123 "
Could someone please point me in the right direction as to why this works fine for numbers but not alphabets?
And what do I use to change the value of the variable? I am currently using "eval" but this again seems to only work with numbers
[ ! -z $2 ] && eval key${ID}_end=$3
Would really appreciate any advice / pointers with this.\
Thank you
You most likely going to have to use an intermediary variable:
key1_begin=abc
var="key${ID}_begin"
echo "$var=${!var}"
Just as this (assume bash):
#! /bin/sh
ID=2
source mychild.sh
keyid="key${ID}_begin"
echo "key${ID}_begin = ${!keyid}"
In short, in bash, you can
var=${!i}
Or on other shells:
var=`eval echo \$$i`

Resources