Reputation: 37
I'm having issues with the cut command,
This is an example of the original file:
userid1:john doe smith:group1
userid2:jane doe smith:group2
userid3:paul rudd :group2
The expected result:
userid1
userid2
userid3
Testing command:
#!/bin/bash
clear
id=`id -u`
if [ $id -eq 0 ]
for i in `cat users.txt`
do
userid=`echo $i|cut -d":" -f1`
echo "$userid" >>temp.txt
`chmod 777 temp.txt`
done
This gives me this output:
userid1
doe
smith
userid2
doe
smith
userid3
rudd
If I erase the space between the name and last name, it works, and I'm specifying ":" as delimiter, dunno what I'm missing.
How can I get the expected result?
Cheers.
UPDATE: Fixed changing for loop to while...read.
Upvotes: 0
Views: 109
Reputation: 835
The issue is with your bash script.
for i in `cat users.txt`
The above line splits your text up by spaces. Just do:
cut -d":" -f1 < users.txt >>temp.txt
chmod 777 temp.txt
If for some reason you bash to process each line individually (perhaps because you need to do something with the userid rather than just output it), you can use read
as in the following:
while read i
do
userid=`echo $i|cut -d":" -f1`
echo "$userid" >>temp.txt
chmod 777 temp.txt
done < users.txt
Upvotes: 2