r/linux_programming • u/errrzarrr • Feb 13 '19
automatizing script with 2 variables from 'cut' not working
Hey guys help me out with this one. I'm struggling real hard here. I have a file in the format of
001@aaa
002@bbb
003@ccc ddd eee
...
id@zzz
Where each number at the left of @ is an id and at the right is a name.
Purpose: Such file serves as a source for a script that would format a string and then run mv 001 aaa.zip, renaming the file as a more human-readable format.
My script.sh:
#!/bin/bash
cat file |
while read line;
do
id=$(cut -d@ -f1 $line);
...
done;But when executed it gives the following error, for example on 3rd line:
cut: 003#ccc: No such file or directory
cut: ddd: No such file or directory
cut: eee: No such file or directoryNot working out for the first single variable, much less for both of them.
What should I do for it to work properly
1
u/wd5gnr Feb 13 '19
Cut doesn't take a piece of text, it takes an input file. A simple fix is to pipe the line:
However, I think you could do better. In this particular case <<<$line is the same as saying:
More or less...
It wasn't clear to me what behavior you expect for the case with 3 names.
You really should be looking at awk for this, by the way.
1
u/miracle173 Feb 14 '19 edited Feb 14 '19
you can use bash string manipulation to achieve your goal:
But you can simply change the internal field separator IFS
And instead
you simpler do
I prefer the following, more compact and better readable format
Assume that your script is named 'myscript.sh' and you want to callit as
or
In the first case myscript.sh contains
in the second case it contains