with bash putting text line to array [closed] - bash

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 9 years ago.
Improve this question
I've a txt file like this:
cat fruits.txt
apple
banana
mango
I need put them to bash array:
fruit[0]='apple'
fruit[1]='banana'
ftuit[2]='mango'

You can do:
fruit=( $(<fruits.txt) )
set | grep fruit
fruit=([0]="apple" [1]="banana" [2]="mango")

In bash 4 and later:
mapfile fruit < fruits.txt
To ignore the trailing newline from each line
mapfile -t fruit < fruits.txt
The command readarray is a synonym for mapfile.

Related

how to cut specific word from a string in BASH [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 5 months ago.
Improve this question
I have a string in shell script:
string1="0101122100635014,TEST123 22 SEP 06 PQR BC,14,25,0.05,,0915-1530|1815-1915:17,2022-09-30,1665066600,ABC:TEST123629500AB,10,11,90014,TEST123,26009,29500.0,BC"
I want to extract ABC:TEST123629500AB in shell scripting.
echo $string1 | magical command
output: ABC:TEST123629500AB
echo "$string1" | cut -d',' -f10
cut will give you part of string.
-d define the separator.
-f Specifies the column you want based on the separator

bash shell execution [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 using
sed -s n v
Nothing works for me
The -i flag is only in GNU Sed.
cat file | tr ']' '[' > temp
mv temp file
The above should work for you.

Replace and remove characters in string, and add output as new column [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 3 years ago.
Improve this question
I have an output from a program that I would like to process and If I pipe it to a file I get:
file/path#backup2018
file2/path/more/path/path#backup2019
file3/path#backup2017
And I want to process it so it looks like this:
file/path file.path
file2/path/more/path/path file.path.more.path.path
file3/path file.path
I have figured out how to make it with separate commands but would like a one liner.
$ awk -F# '{s=$1; gsub("/", ".", s); print $1, s}' file | column -t
file/path file.path
file2/path/more/path/path file2.path.more.path.path
file3/path file3.path
using sed
sed 's/\([^#]*\)#.*/\1 \1/g' file|column -t

Sed to edit multiple patterns [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 6 years ago.
Improve this question
Is there a way to change multiple (up to ten) patterns in a single fasta file using sed?
For instance I want to change X for Y:
sed "s/X/Y/g" < file1.fasta > output.fasta
how to add sed "s/\s/_/g" and 8 more commands to the same one-liner?
You can separate the commands by semicolons
sed 's/a/b/;s/c/d/'
(you can also use newlines instead of semicolons)
or you can use multiple -es:
sed -e 's/a/b/' -e 's/c/d/'
see this example: (test with gnu sed):
kent$ echo 'abcd'|sed 's/a/1/;s/b/2/;s/c/3/;s/d/4/'
1234
kent$ echo 'abcd'|sed 'y/abcd/1234/'
1234

insert the content between matching pattern in unix [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 6 years ago.
Improve this question
I want to insert the content between the matching pattern in shell please help on this
For example :
file called input.txt :
var a = [ ]
file called output.txt :
1152
1185
1185
I want to insert the values from output.txt file to the file input.txt.
it should be like var a = [1152 1185 1185]
sed "s/\[/[ $(xargs < output.txt)/" input.txt
xargs < filename dumps all file lines in just one, i.e., replaces EOL characters for SPACE characters.
$(xargs < filename) expands to the contents of the filename in just one line. Hence the use of double quotes, not single ones.

Resources