I am trying to make a file but I keep getting this error
tr: extra operand ' '
./homework: line2: syntax error near unexpected token '|'
./homework: line '| tr ' \t' '\n\n' \ '
Here is my file contents
tr ['a-z'] ['A-Z'] < practice \
| tr ' \t' '\n\n' \
| sed '/^$/d' \
| sort \
| tee x-sor.out
The error is said to be in line 2 and I cant tell what the problem is or apparently syntax
But if anyone is curious yes it is tr ' \t' that's what was used from my textbook
Related
I have a string Contain key=value format separated by #
I am trying to replace the '=' char occurrences with ':' in the value of TITLE using BASH script.
"ID=21566#OS=Linux#TARGET_END=Synchronica#DEPENDENCY=Independent#AUTOMATION_OS=Linux#AUTOMATION_TOOL=JSystem#TITLE=Session tracking. "DL Started" Status Reported.Level=none"
later on i am parsing this string to execute the eval operation
eval $(echo $test_line | sed 's/"//g' | tr '#' '\n' | tr ' ' '_' | sed 's/=/="/g' | sed 's/$/"/g')
When the sed 's/=/="/g' section will also change ..Level=none to
Level="none
This leads to
eval: line 52: unexpected EOF while looking for matching `"'
What will be right replace bash command to replace my string ?
As an alternative, consider pure-bash solution to bring the variables into bash, avoiding the (risky) eval.
IFS=# read -a kv <<<"ID=21566#OS=Linux#TARGET_END=Synchronica#..."
for kvp in "${kv[#]}" ; do
declare "$kvp"
done
I found the way to solve it.
I will add sed 's/=/:/8g' to my eval command.
It will replace 8th to nth occurrences of '='.
The action will only effect the value of TITLE as expected.
eval $(echo $test_line | sed 's/=/:/8g' | sed 's/"/"/g' | tr '#' '\n' | tr ' ' '_' | sed 's/=/="/g' | sed 's/$/"/g')
I did it like this :
echo '"ID=21566#OS:Linux#TARGET_END:Synchronica#DEPENDENCY:Independent#AUTOMATION_OS:Linux#AUTOMATION_TOOL:JSystem#TITLE:Session tracking. "DL Started" Status Reported.Level=none"' \
|
sed -E 's/(#)?([A-Z_]+)(=)/\1\2:/g'
Let me know if it works for you.
I have a simple shell script for installing Jenkins plugins:
installPlugin() {
{...}
}
for f in ${plugin_dir}/*.hpi ; do
#without optionals
deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | tr '\n' ' ' )
for plugin in $deps; do
installPlugin "$plugin" 1 && changed=1
done
done
I'm calling this script from a Dockerfile like so:
RUN JENKINS_HOME=$JENKINS_HOME \
http_proxy=$http_proxy \
https_proxy=$https_proxy \
$JENKINS_HOME/install_plugin.sh \
ace-editor:1.1 \
Even though the unzip routine is not installed in the Dockerfile, when I run docker build the output is still SUCCESSFUL even though unzip is missing. I would like to fail the build if the following step fails:
deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | tr '\n' ' ' )
Any help is much appreciated!
LE
The script outputs the following:
/var/jenkins/install_plugin.sh: line 62: unzip: command not found
deps=
However, the docker build doesn't fail even if that script failed:
Successfully built 54f5a5ec567d
I ended up doing an empty check like this:
for f in ${plugin_dir}/*.hpi ; do
manifestMf=$(unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r')
if [ $manifestMf=="" ]; then
echo "Invalid META-INF/MANIFEST inside $f"
exit 1
fi;
#without optionals
deps=$(echo $manifestMf | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | tr '\n' ' ' )
# with optionals
#deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | tr '\n' ' ' )
for plugin in $deps; do
installPlugin "$plugin" 1 && changed=1
done
done
done
Below is the command I'm typing
cat program.c | tr '\n' ' ' | tr '\t' ' ' | tr -s [:space:] ' ' >unreadable.c
The output is
Hi w ssup. How re you doing bro Its nice to see you g in bro .
But the problem is character 'a' is getting removed from the file which should not happen.
Can anyone pls help.
Thanks
[:space:] is a valid glob pattern (matching a single ', s, p, a, c, or e), and you appear to have a file named a in your working directory that the pattern matches, so you are really running tr -s a ' ' instead after the shell performs pathname expansion. Quote the pattern to prevent the shell from exanding it.
... | tr -s '[:space:]' ' ' > unreadable.c
(Note that if there was no matching file, the default behavior is to treat the pattern literally, and your command would work as expected. However, there is a shell option you can set to treat non-matching patterns as an error, so it is good practice to always quote an argument to guarantee it is passed literally to a command.)
[:space:] already includes newlines and tabs, so you only need the one call to tr:
tr '[:space:]' ' ' < program.c > unreadable.c
you can use sed like this:
cat program.c | sed -r "s/\n|\t|\s+/ /g" > unreadable.c
I have an ouput from the command line, i need to trim and get the desired output as shown below:
Input:
['0x66']
['0x66', '0x137', '0xa9']
[]
['0x148', '0x11a', '0x167', '0x151', '0xe6']
[]
['0x171', '0xe2', '0x174']
Output:
0x66
0x66
0x137
0xa9
0x148
0x11a
0x151
0xe6
I used: tr -d "[]'," but after removing those do linux has any command like .split() in python.
[EDIT] After looking at the man pages of tr, I see there is a translate option so I piped the whole ouptut to:
output | tr -d "[]' | tr " " "\n"
I suggest using simple grep -o (show only matching text):
grep -o "0x[^']*" file
0x66
0x66
0x137
0xa9
0x148
0x11a
0x167
0x151
0xe6
0x171
0xe2
0x174
How about piping to this:
| tr -d "[]' " | tr ',' '\n' | sed -n '/^.\+$/p'
It first deletes useless chars, then "splits" the fields to their own lines and then removes any empty ones.
With tr and a little help from sed:
tr -d "[]' " <file.txt | sed -n '/./ p' | tr ',' '\n'
tr -d "[]' " removes all[,]`, space and single quotes from the data
sed part matches lines with at least one character to leave out empty lines
Then tr ',' '\n' converts all commas to newlines
% cat file.txt
['0x66']
['0x66', '0x137', '0xa9']
[]
['0x148', '0x11a', '0x167', '0x151', '0xe6']
[]
['0x171', '0xe2', '0x174']
% tr -d "[]' " <file.txt | sed -n '/./ p' | tr ',' '\n'
0x66
0x66
0x137
0xa9
0x148
0x11a
0x167
0x151
0xe6
0x171
0xe2
0x174
I have a file which has contents like the below
SPEC.2.ATTRID=REVISION&
SPEC.2.VALUE=5&
SPEC.3.ATTRID=NUM&
SPEC.3.VALUE=VS&
I am using the below command to extract only the numbers from the first line. Is this way efficient or you guys think of an alternate way ?
cat ticketspecdata | tr -d " " | tr -s "[:alpha:]" "~" | tr -d "[=.=]" | cut -d "~" -f2
Using grep :
$ grep -om1 '[0-9]\+' file
2
Or
head -n1 file | tr -cd '[:digit:]'
You may also want to read about UUOC:
http://porkmail.org/era/unix/award.html