Copy word from 1 file to another using Bash - bash

I'm trying to copy over only specific words from 1 text file to another.
this is what is inside my username2.txt:
jason,visitor
mark,staff
orlando,visitor
I'm trying to copy over only jason and orlando to another txt file so it'll just be like:
jason
orlando
this is currently my bash script (as of right now, it only creates a blank txt file):
#!/bin/bash
username="username2.txt"
while IFS=, read username1 group1; do
if [ $group1 = "visitor" ]; then
awk $username1 $username > reportvisitors.txt
fi
done < $username

This could be easily done with awk and we do not need to use a while loop for this since awk itself could read Input_file itself.
username="username2.txt"
awk -F, '$2=="visitor"{print $1}' "$username" > "reportvisitors.txt"
Explanation: Simple explanation is, making field separator as , for all lines of Input_file(which is a shell variable named username). Then inside main program checking condition $2=="visitor" to check if 2nd field is visitor then printing 1st column. At last of this awk program sending output to reportvisitors.txt.

Replace
awk $username1 $username > reportvisitors.txt
with
echo "$username1" >> reportvisitors.txt
Update
#!/bin/bash
username="username2.txt"
while IFS=, read username1 group1; do
if [ "$group1" = "visitor" ]; then
echo "$username1"
fi
done < $username > reportvisitors.txt

You have to edit the line 7 to
echo $username1 >> reportvisitors.txt
The Code should look like
#!/bin/bash
This part makes sure that an already existing file with the name is deleted
FILE=reportvisitors.txt
if test -f "$FILE"; then
rm $FILE
fi
from here on I have only edited line 5
username="username2.txt"
while IFS=, read username1 group1; do
if [ $group1 = "visitor" ]; then
echo $username1 >> reportvisitors.txt
fi
done < $username

Related

How to edit a particular line based on the input I give in Unix Scripting

I have wrote a program in bash, where there will be details of the students in a file and on passing input as roll no to it I need to edit that particular line in the file.
But when I tried by passing the roll no, it is displaying the whole file to me for editing when condition matches.
Here is my program:
#!/bin/bash
input="/home/kalyan/Desktop/Exercise/studet.txt"
while IFS=":" read -r rollno name s1 s2 s3
do
echo "$rollno"
done < "$input"
read -p "Enter the Roll No:" rollno1
if [ "$rollno"-eq"$rollno1" ]; then
nano studet.txt
cat studet.txt
else
echo "Doesn't match"
fi
or this program
#!/bin/bash
file="/home/kalyan/Desktop/Exercise/"
FILE="/home/kalyan/Desktop/Exercise/studet.txt"
echo "file to be processed $FILE"
cat $FILE
echo "Reading Roll Number:"
read -r rollno;
if grep $rollno /home/kalyan/Desktop/Exercise/studet.txt
then
echo "Roll No exists"
else
echo "Roll No doesn't exists"
fi
Here I'm trying to get a record of a student based on the roll no as input.
when the roll no matches I'm displaying his rollno, name, marks of three subjects(s1,s2,s3).
Now I need to edit the line which matches the criteria.
Not sure I understand fully what you are doing, but I would do (based on title of the question):
line_number=`grep -n "^$rollno\$" /tmp/a | cut -d : -f 1 "$input"`
nano +"$line_number" studet.txt
Is this last file the same as $input?
I'am not sure if this what you want to have... but say that you want to change a user name depending on its id given as input while running the program:
studet.txt
1:surname1:name1:age1
2:surname2:name2:age2
3:surname3:name3:age3
script.sh
#!/bin/bash
read -p "Enter the Roll No:" rollno
read -p "Enter new name:" name
awk -F ":" -v roll="$rollno" -v name="$name" 'BEGIN {OFS = ":"} $1==roll {$3=name}1' studet.txt > testfile.tmp && mv testfile.tmp studet.txt
The above script will ask the user to pass rollno (exp 1) and name values.
Then awk commad will check if the given rollno exists in the file first fileds (file will be splited into fields using : separator), if this rollno exists the third field of the same line will be changed by the value name the user passed before.
Give it a try by passing 1 and then some string and then check your file.

While loop issue on second column using IFS

This seems simple, list the directory from the first field then list the directory from the second field. The fields from the input file are comma separated, e.g.: XXXX1111111111112222,cool.com.
I run the command:
./list_directories some_file.csv
The list_directories script is this:
#!/bin/bash
INPUT=$1
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read WORKING STORE
do
echo $STORE
ls $STORE
echo $WORKING
ls $WORKING
done < $INPUT
IFS=$OLDIFS
Here's the output:
/pathtothe/som/coolplace/Imlookingfor/cool.com/place/123/XXXX1111111111112222 : No such file or directorye/Imlookingfor/cool.com/place/123/XXXX1111111111112222 /pathtothe/som/coolplace/Imlookingfor/cool.com/placing/123/XXXX1111111111112222 fileindir.txt otherfileindir.txt lastofthefilesindir.txt
I know that both directories exist. Not sure if I'm getting caught up on the loop or on the IFS.

how to parse values from text file and assign it for shell script arguments

I have stored some arguments value in sample.txt
1 >> sample.txt
2 >> sample.txt
3 >> sample.txt
I have tried to parse the sample.txt in a shell script file to collect and assign the values to specific variables.
#!/bin/sh
if [ -f sample.txt ]; then
cat sample.txt | while read Param
do
let count++
if [ "${count}" == 1 ]; then
Var1=`echo ${Param}`
elif [ "${count}" == 2 ]; then
Var2=`echo ${Param}`
else
Var3=`echo ${Param}`
fi
done
fi
echo "$Var1"
echo "$Var2"
echo results prints nothing. I would expect 1 and 2 should be printed. Anyone help?
You are running the while loop in a subshell; use input redirection instead of cat:
while read Param; do
...
done < sample.txt
(Also, Var1=$Param is much simpler than Var1=$(echo $Param).)
However, there's no point in use a while loop if you know ahead of time how many variables you are setting; just use the right number of read commands directly.
{ read Var1; read Var2; read Var3; } < sample.txt

Variables from file

A text file has the following structure:
paa pee pii poo puu
baa bee bii boo buu
gaa gee gii goo guu
maa mee mii moo muu
Reading it line by line in a script is done with
while read LINE; do
ACTION
done < FILE
I'd need to get parameters 3 and 4 of each line into variables for ACTION. If this was manual input, $3 and $4 would do the trick. I assume awk is the tool, but I just can't wrap my head around the syntax. Halp?
read does this just fine. Pass it multiple variables and it will split on $IFS into that many fields.
while read -r one two three four five; do
action "$three" "$four"
done <file
I added the -r option because that is usually what you want. The default behavior is a legacy oddity of limited use.
Thanks tripleee. In the meantime I managed a suitably versatile solution:
#!/bin/sh
if [ ! $1 ]; then
echo "Which inputfile?"
exit
elif [ ! $2 -o ! $3 ]; then
echo "Two position parameters required"
exit
fi
if [ -f outfile ]; then
mv outfile outfile.old
fi
while read -a LINE; do
STRING="${LINE[#]}"
if [ ${LINE[$2-1]} == ${LINE[$3-1]} ]; then # remove comment for strings
# if [ ${LINE[$(($2-1))]} -eq ${LINE[$(($3-1))]} ]; then # remove comment for integers
echo $STRING >> outfile
fi
done < $1

reading text file - bash

I am trying to read a text file-"info.txt" which contains the following information
info.txt
1,john,23
2,mary,21
what I want to do is to store each columns into a variable and print any one of the columns out.
I know this may seems simple to you guys but I am new to writing bash script, I only know how to read the file but I don't know how to delimit the , away and need help. Thanks.
while read -r columnOne columnTwo columnThree
do
echo $columnOne
done < "info.txt"
output
1,
2,
expected output
1
2
You need to set the record separator:
while IFS=, read -r columnOne columnTwo columnThree
do
echo "$columnOne"
done < info.txt
Is good to check if the file exists too.
#!/bin/bash
INPUT=./info.txt
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read -r columnOne columnTwo columnThree
do
echo "columnOne : $columnOne"
echo "columnTwo : $columnTwo"
echo "columnThree : $columnThree"
done < $INPUT
IFS=$OLDIFS

Resources