How can I manipulate text in bash? [closed] - bash

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 5 years ago.
Improve this question
How can I manipulate text in bash using either awk, grep, perl or sed?
Input:
ted foo,bar,zoo
john ket,ben
Expected Output:
foo,ted
bar,ted
zoo,ted
ket,john
ben,john

With awk:
awk 'BEGIN{FS="[ ,]"; OFS=","} {for (i=2; i<=NF; i++) print $i,$1}' file
Output:
foo,ted
bar,ted
zoo,ted
ket,john
ben,john
See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Related

Concatenate the output of multiple cuts in one line in shell script [closed]

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 2 years ago.
Improve this question
I am trying to concatenate the output of three cut functions with - in a shell script in a single line. I tried as below, but wont work. How do I do this ?
echo "$(cut -d',' -f2 FILE.csv)-$(cut -d',' -f1 FILE.csv)-$(cut -d',' -f3 FILE.csv)"
Using awk to change the delimiter:
awk -F, '{ print $2, $1, $3 }' OFS='-' FILE.csv
Or with csvkit commands (Especially useful if your file has more complex CSV with things like commas in quoted fields or multi-line fields that a naive split on comma can't handle correctly):
csvcut --columns 2,1,3 FILE.csv | csvformat -D'-'

How do I print out the longest word in a file that appears at least 10 times [closed]

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
Exactly what the title says: what is the bash command for printing out the longest word in a text file that appears at least 10 times.
Try this Denis:
tr -s " " "\n" < file | while read -r l; do echo "${#l} $l"; done | sort -n | awk '$1 >= 10 ' | awk '{print $2}' | tail -n1

how to pass text file as argument in shell script [closed]

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 2 years ago.
Improve this question
the text file contains
from:sender#gmail.com
to:receiver#gmail.com
subject:
attachment:asdfg.xlsx
all arguments should be handled in shell script
I tried but if subject contains space then it gives problem
from=$(echo $1|cut -d ":" -f 2 <<< "$1")
to=$(echo $2|cut -d ":" -f 2 <<< "$2")
subject="$3"
attachment=$(echo $4|cut -d ":" -f 2 <<< "$4")
When you could read the Input_file then passing it as a variable will not be a good option IMHO, so create variables inside script by reading Input_file, opting OP's method of creating variables but enhancing code to awk.
from=$(awk -F':' '/from/{print $NF}' Input_file)
to=$(awk -F':' '/^to/{print $NF}' Input_file)
subject=$(awk -F':' '/^subject/{if($NF){print $NF} else {print "NULL subject"}}' Input_file)
attachment=$(awk -F':' '/^attachment/{print $NF}' Input_file)

How to filter records from a file using bash script [closed]

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
I'm supposed to filter records from multiple files. Files are delimited by |. On the 24th field, I will filter the records by "9120". How am I supposed to filter the files by using bash script?
20|09328833007|0|0|9222193385|0|GS|515051032704315|0|*HOME||20140311|101640|0|0|‌​||12|18|0|0|1||3100|00886FC0||0|0|| |||N|N|||N||||N|||||| 301131301|||||||||||11|0||00|FF|11|FF|140311101640|352912058113130000||CEBS1|MSH‌​15
The more concise way using awk:
awk '$24=="9120"' FS='|' file*
Using variable input:
awk -v col=24 -v value="9120" '$col==value' FS='|' file*
awk is useful to processing files like this. You set the FIELD SEPARATOR to |:
To print the 24th field:
$ awk -F '|' '{ print $24 }' sample.txt
3100
To print lines where the 24th field is the value you specified:
awk -F '|' '$24=="9120" { print; }' sample.txt
Try this:
cat file* | awk -F"|" '$24=="9120" {print $0}'

Print Record Number with a prefix [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a shell script that creates a CSV output file like below (on separate lines) :
aaa,111
bbb,222
ccc,999
I want to have a record number as the first column in the above output such as,
dm1,aaa,111
dm2,bbb,222
dm3,ccc,999
How do I create a variable for dm within my shell script?
Use awk:
awk '{print "dm" NR "," $0}' input.csv >output.csv
or
... | awk '{print "dm" NR "," $0}' >output.csv
You can use a var and expr to do some maths like
dm=`expr $dm + 1`
echo "dm$dm,...rest of output..."
Pipe your output to awk
yourcommand | awk -F, -v prefix="dm" '{printf "%s%d,%s\n", prefix, NR, $0}'
we set -F, to set the input field separator, and the -v to set a prefix. You can replace "dm" with a shell variable, if you want.

Resources