Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am completely new to bash and am trying to practice with various simple tasks. If given a text file, how can I output the words in the file with their frequencies? For example if the text file contained "I really really love burgers" the output should look like:
Burgers 1
I 1
Love 1
Really 2
I am unsure how to even get started. How do you load text files into bash? And how do you put things? Sorry for the very beginner questions but I would really appreciate some help, thanks!
You can do what you want by this way:
Take the input text file
Take the input Directory to save a file of words in it
Print the unique words with their frequencies.
#!/bin/bash
echo -e "Suggested files: \n"
ls
echo -e "\n"
read -p "Enter the file name :" file
if [ -e $file ]
then
if [ -s $file ]
then
echo -e "Enter the directory where do you want to save the file in \n"
read dir
words=$(grep -v '^$' $file|tr " " "\n"|sort|uniq)
grep -v '^$' $file|tr " " "\n" |sort| grep -v '^$' > $dir/file[$j].sorted
for i in $words
do
num=$(grep -c "$i" $dir/file[$j].sorted)
echo -e "The word \e[1;33m$i\e[0m exists \e[1;34m$num\e[0m times in the file \n"
done
else
echo "The file you have entered is empty"
fi
else
echo "The file you have entered does not exist"
fi
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Please excuse me for my amateur language.
I have written a simple one line code to cat out content I want from a huge text file:
cat file | grep "value" | cut -f1 -d":"
It out put lines of paths of file from there on.
I want it to go on doing this:
cd into the paths one line at a time.
each time after cd run this command:
ls | grep .fileformat
Then let me run 3 commands I choose to, if I press [return] with no value it will ignore and listen for the next command. After three is done it will go on.
cd into the directory of the next line and repeating until the last line.
Sorry I couldn't figure this later part out as I didn't know where to start googling even.
Thank you for looking!
Edit 1: output of my initial command gives paths like this:
/home/user/path/to/file
So there is no ~, it should be able to cd into them no problem?
A slightly modified version of Allan Wind's answer that does what I THINK the OP wants (tried to reply as a comment to Allan but couldn't figure out code formatting properly):
sed -n '/value/s/\([^:]*\):.*/\1/p' file | while read d
do
echo -e "Entering $d"
cd $d
ls | grep .fileformat
for i in {1..3}; do
echo -e "Type your command #${i}:\n"
read -p '>' input < /dev/tty
[ -z "$input" ] && echo -e "\nDoing nothing this time...\n" && continue
eval "$input"
done
done
(Usual caveats of reading interactively + using eval being dangerous)
Here is the skeleton of an answer to help you ask a better question:
sed -n '/value/s/\([^:]*\):.*/\1/p' file | while read d
do
(
cd d
ls | grep .fileformat
read -p "3 commands? " c
[ -z "$c" ] && continue
cd d
eval "$c"
)
done
(Usual caveats of reading interactively + using eval being dangerous)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a file with contents like this
adv_dir=2
ami_pro=3
I have a variable $TEMP with value ami_pro, I want to increase the value of $TEMP by 1.
Want a bash script for this.
State of file after updates
adv_dir=2
ami_pro=4
Icky-ish, spooky indirection, but...
$: echo $TEMP
ami_pro
$: echo $ami_pro
4
$: (($TEMP++)) # $TEMP evals to ami_pro, which gets incremented
$: echo $TEMP
ami_pro
$: echo $ami_pro
5
$: let $TEMP++ # same, likely in any arithmetic context
$: echo $ami_pro
6
so...
TEMP=ami_pro # just setting in the env
grep "$TEMP=" file > $TEMP.tmp # grap the line we need to edit
. $TEMP.tmp # souce it to set the var
sed -i "s/$TEMP=${!TEMP}/$TEMP=$((++$TEMP))/" file # in-place edit
You could skip the tempfile with eval "$(grep "$TEMP=" file)" but eval makes me itch.
The sed is an in-place edit of file using double-quotes to allow the OS to pre-process the vars before making the update.
TEMP=ami_pro
so
$TEMP=${!TEMP}
is parsed by the OS into
ami_pro=$ami_pro
Since we sources the line that said ami_pro=3, that gets further parsed into
ami_pro=3
then
$TEMP=$((++$TEMP))
becomes ami_pro=$((++ami_pro)) which processes to
ami_pro=4
all sed gets is the result strings, so by the time sed starts parsing, what it sees is
s/ami_pro=3/ami_pro=4/
Once that edit is handled, we can break out of the loop.
You can use awk for that:
#!/bin/bash
TMP_FILE=$(mktemp)
awk -F= "/^${TEMP}=/ {print \$1 \"=\" \$2 + 1 \"*\"; next} 1" "${1}" > ${TMP_FILE} && mv ${TMP_FILE} "${1}"
rm ${TMP_FILE}
Pass the file with the data as a parameter to this script.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In Bash, I would like to know how to read lines from a file in a directory i specifiy so that there are no arguements along with running the script. All I have seen if suggestions for running a script with a file given as the arguement rather than one specified.
while read -r line; do
echo "$line"
done < filename
% cat read_line_by_line_example.txt_inside
dir="$1"
[ -f "$dir"/example.txt ] || exit 1
# borrowing from Cyrus
while read -r line; do
echo "$line"
# do watever you want with "$line"
done < "$dir"/example.txt
% sh read_line_by_line_example.txt_inside "a/b/c/d/my dir"
line 1
line b
last line
%
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a very long pattern file and medium-length text file. I simply want to know if the strings in the pattern file are present or not -- I don't care what line they're on. Is there a way to track which patterns are found and which not?
You can do something like this:
while read line; do
grep -q "$line" textFile
echo "${line}: $?"
done < patternFile
Loop over the patternFile and for every pattern invoke a grep -q on the textFile. grep -q will not produce any output, but it will set bash's exit status to 0 if the pattern was found and to 1 if it was not found.
As commented by that other guy, you can get a list with all matching patterns like this:
while read line; do
grep -q "$line" textFile && echo "$line"
done < patternFile
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want store in a file and in a variable multiples lines from a "paste" via shell script. A simple read terminates after the first line.
How can I accomplish that?
Example:
echo "Paste the certificate key:"
1fv765J85HBPbPym059qseLx2n5MH4NPKFhgtuFCeU2ep6B19chh4RY7ZmpXIvXrS7348y0NdwiYT61
1RkW75vBjGiNZH4Y9HxXfQ2VShKS70znTLxlRPfn3I7zvNZW3m3zQ1NzG62Gj1xrdPD7M2rdE2AcOr3
Pud2ij43br4K3729gbG4n19Ygx5NGI0212eHN154RuC4MtS4qmRphb2O9FJgzK8IcFW0sTn71niwLyi
JOqBQmA5KtbjV34vp3lVBKCZp0PVJ4Zcy7fd5R1Fziseux4ncio32loIne1a7MPVqyIuJ8yv5IJ6s5P
485YQX0ll7hUgqepiz9ejIupjZb1003B7NboGJMga2Rllu19JC0pn4OmrnxfN025RMU6Qkv54v2fqfg
UmtbXV2mb4IuoBo113IgUg0bh8n2bhZ768Iiw2WMaemgGR6XcQWi0T6Fvg0MkiYELW2ia1oCO83sK06
2X05sU4Lv9XeV7BaOtC8Y5W7vgqxu69uwsFALripdZS7C8zX1WF6XvFGn4iFF1e5K560nooInX514jb
0SI6B1m771vqoDA73u1ZjbY7SsnS07eLxp96GrHDD7573lbJXJa4Uz3t0LW2dCWNy6H3YmojVXQVYA1
v3TPxyeJD071S20SBh4xoCCRH4PhqAWBijM9oXyhdZ6MM0t2JWegRo1iNJN5p0IhZDmLttr1SCHBvP1
kM3HbgpOjlQLU8B0JjkY8q1c9NLSbGynKTbf9Meh95QU8rIAB4mDH80zUIEG2qadxQ0191686FHn9Pi
read it and store it file say /tmp/keyfile
read it and store it in a variable $keyvariable
You just have to decide how much to read.
If this is the only input, you could read until end of file. This is how most UNIX utilities work:
#!/bin/bash
echo "Pipe in certificate, or paste and it ctrl-d when done"
keyvariable=$(cat)
If you want to continue reading things later in the script, you can read until you see a blank line:
#!/bin/bash
echo "Paste certificate and end with a blank line:"
keyvariable=$(sed '/^$/q')
If you want it to feel more like magic interactively, you could read until the script has gone two seconds without input:
#!/bin/bash
echo "Paste your certificate:"
IFS= read -d '' -n 1 keyvariable
while IFS= read -d '' -n 1 -t 2 c
do
keyvariable+=$c
done
echo "Thanks!"