Score:1

grep and awk commands not returning or storing output to the variable

tw flag

I have a (users.csv) file containing a list of id's of all employees. I need is to loop through this file for each employee, search if it exists in backup.txt file using grep and store the output in a variable (user_data) for printing.

I tried echo the value of user_data, but its printing as empty, wich reason why is user_data returning empty? Although user_id does contain the id of an employee, I believe it is not working because of this loop.

My script:

#!/bin/bash

header=true

while IFS=',' read -r user_id; do

if $header; then
        header=false
        continue
    fi

  user_data=$(grep "uid=$user_id" disable_Backup.txt)
  echo "User is $user_id"
  echo "Data is $user_data"

done < users.csv

Contents of users.csv:

user_id
x220988
x240755
x260698

Contents of disable_Backup.txt:

uid=x220988
inetUserStatus=Active
EmployeeStatus=ACT

uid=x240755
inetUserStatus=Active
EmployeeStatus=ACT

uid=x260698
inetUserStatus=Active
EmployeeStatus=ACT

What do I need to do to store the output from grep into user_data variable?

hr flag
Does your users.csv file have Windows line endings (CRLF)? Check with command `file users.csv`
Haider Ali Syed avatar
tw flag
btln000266:infra$ file users.csv users.csv: ASCII text, with CRLF line terminators
hr flag
So you are trying to match `uid=x220988<CR>` against `uid=x220988` and it's not matching
Haider Ali Syed avatar
tw flag
yes, its matching if I run the same grep outside this file but inside the loop it seem the grep is returning empty, nothing is being printed when I echo user_data
hr flag
So when you run the same grep outside this file, how are you assigning value(s) to `user_id`? by `read`ing them from the users.csv file?
Haider Ali Syed avatar
tw flag
running it in a separate shell script file by hard-coding the value of user_id ``` #!/bin/sh user_id="x220988" user_data=$(grep "uid=$user_id" disable_Backup.txt) echo "The value of uid for user $user_id is $user_data" ``` Below is the output of above which I want in my orignal script. ./test.sh The value of uid for user x220988 is uid=x220988
Score:2
hr flag

Your users.csv file has Windows-style line terminators (CRLF), whereas your disable_Backup.txt file apparently has Unix-style (LF).

When you IFS=',' read -r user_id from users.csv, the user_id variable will contain the trailing CR character - so when you try to match it against lines from disable_Backup.txt - which has no such characters - the match fails.

Either make sure both files have the same line endings (the easiest way is using dos2unix or unix2dos), or strip off the carriage return character before you pass the value to grep:

user_data=$(grep "uid=${user_id%$'\r'}" disable_Backup.txt)
Haider Ali Syed avatar
tw flag
Thanks a lot, that worked :)
Raffa avatar
jp flag
@HaiderAliSyed Please see [What should I do when someone answers my question?](https://askubuntu.com/help/someone-answers)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.