Change output of whoami to uppercase in shell - shell

I am trying to save the output of whoami to a different variable after changing it to uppercase. Please help me. I am newbie to shell scripting.
Thanks in advance.

Using pure bash
x=$(whoami)
v=${x^^}
Using tr
v=$(whoami | tr 'a-z' 'A-Z')
Or
v=$(whoami | tr [:lower:] [:upper:])
Using awk
v=$(whoami | awk '{print toupper($0)}')
Using perl
v=$(whoami | perl -e 'print uc <>')

Related

One-liner POSIX command to lowercase string in bash

Problem
I have this comand:
sed $((SS - default_scripts))!d customScripts.txt
and it gives me Foo Bar.
I want to convert this to lowercase.
Attempt
When I tried using the | awk '{print tolower($0)}' command on it it returned nothing:
$($(sed $((SS - default_scripts))!d customScripts.txt) | awk '{print tolower($0)}')
Final
Please enlighten me on my typo, or recommend me another POSIX way of converting a whole string to lowercase in a compact manner. Thank you!
The pipe to awk should be inside the same command substitution as sed, so that it processes the output of sed.
$(sed $((SS - default_scripts))!d customScripts.txt | awk '{print tolower($0)}')
You don't need another command substitution around both of them.
Your typo was wrapping everything in $(...) and so first trying to execute the output of just the sed part and then trying to execute the output of the sed ... | awk ... pipeline.
You don't need sed commands nor shell arithmetic operations when you're using awk. If I understand what you're trying to do with this:
$(sed $((SS - default_scripts))!d customScripts.txt) | awk '{print tolower($0)}'
correctly then it'd be just this awk command:
awk -v s="$SS" -v d="$default_scripts" 'BEGIN{n=s-d} NR==n{print tolower($0); exit}' customScripts.txt

Getting a zsh alias including a pipe to execute

I wanted a command that would quickly copy the current tmux window layout to the clipboard on Mac using zsh. I came up with the following:
tmux list-windows | awk '{print $7}' | sed 's/\]$//' | pbcopy
When I run this from the command line it works perfectly with an output like the following:
d97b,135x32,0,0[135x16,0,0{87x16,0,0,0,47x16,88,0,1},135x15,0,17{87x15,0,17,2,47x15,88,17,3}]
However, I can't seem to run it as an alias. If I add the line:
alias layout="tmux list-windows | awk '{print $7}' | sed 's/\]$//' | pbcopy"
to my .zshrc file when I run layout the command does not work as expected. It instead outputs the full tmux list-windows command with the word layout replacing the session name:
0: layout* (4 panes) [135x32] [layout d97b,135x32,0,0[135x16,0,0{87x16,0,0,0,47x16,88,0,1},135x15,0,17{87x15,0,17,2,47x15,88,17,3}]] #0 (active)
What am I doing wrong?
Thanks.
alex_i is correct, if you escape the $7 everything works.
alias layout="tmux list-windows | awk '{print \$7}' | sed 's/\]$//' | pbcopy"
Note the backslash before the $7.
Don't use an alias; use a function:
layout () {
tmux list-windows | awk '{print $7}' | sed 's/\]$//' | pbcopy
}
Then you don't need to worry about quoting.
Is your '$7' interpreted during the .zshrc loading ? Couldn't it be the issue ?

bash shell script for mac to generate word list from a file?

Is there a shell script that runs on a mac to generate a word list from a text file, listing the unique words? Even better if it could sort by frequency....
sorry forgot to mention, yeah i prefer a bash one as i'm using mac now...
oh, my file is in french... (basically i'm reading a novel and learning french, so i try to generate a word list help myself). hope this is not a problem?
If I understood you correctly, you need something like that:
cat <filename> | sed -e 's/ /\n/g' | sort | uniq -c
This command will do
cat file.txt | tr "\"' " '\n' | sort -u
Here sort -u will not work on Macintosh machines. In that case use sort | uniq -c instead. (Thanks to Hank Gay)
cat file.txt | tr "\"' " '\n' | sort | uniq -c
Just answer my question to dot down the final version i'm using:
tr -cs "[:alpha:]" "\n" < FileIn.txt | sort | uniq -c | awk '{print $2","$1}' >> FileOut.csv
some notes:
tr can be used directly to do replacement.
since i'm interested creating a word list for my french vocabulary, i used [:alpha:]
awk is used to insert a comma, so that the output is a csv file, easier for me to upload...
thanks again for everyone helping me.
sorry i didn't put it clearly at the beginning that i'm using a mac and expect a bash script.
cheers.

issue with using double grave accent (backtick) in bash

I am trying to figure out which files I need to modify; so by that, I use sequences of grep commands. I want to find out which files contain both foo and bar. Therefore, my command is:
grep foo `grep bar * -l` | awk -F':' '{print $1}' | sort | uniq
This command gets me a big list that looks like this:
pageABC.txt
pageBCD.txt
pageDEF.txt
I want this output to be opened in emacs. So what I'd normally do is:
emacs ` whatever_was_in_my_output `
This command normally opens all the files.
If I try
emacs `grep foo `grep bar * -l` | awk -F':' '{print $1}' | sort | uniq `
Emacs won't even start. Maybe it's because of the double grave accents used.
Any ideas how to solve this?
Many Thanks,
D
You forgot to escape the inner command substitution:
emacs `grep foo \`grep bar * -l\` | awk -F':' '{print $1}' | sort | uniq`
In cases like this, I usually prefer the alternative command substition syntax, since it nests more easily:
emacs $(grep foo $(grep bar * -l) | awk -F':' '{print $1}' | sort | uniq)
Avoid backticks in bash, and use $(command) to run sub-commands. They nest properly, unlike backticks.

Extract the last directory of a pwd output

How do I extract the last directory of a pwd output? I don't want to use any knowledge of how many levels there are in the directory structure. If I wanted to use that, I could do something like:
> pwd
/home/kiki/dev/my_project
> pwd | cut -d'/' -f5
my_project
But I want to use a command that works regardless of where I am in the directory structure. I assume there is a simple command to do this using awk or sed.
Are you looking for basename or dirname?
Something like
basename "`pwd`"
should be what you want to know.
If you insist on using sed, you could also use
pwd | sed 's#.*/##'
If you want to do it completely within a bash script without running any external binaries, ${PWD##*/} should work.
Using awk:
pwd | awk -F/ '{print $NF}'
Should work for you:
pwd | rev | cut -f1 -d'/' - | rev
Reference:
https://stackoverflow.com/a/31728689/663058

Resources