Bash Split Line And Get Element

[Solved] Bash Split Line And Get Element | Swift - Code Explorer | yomemimo.com
Question : bash split line and get element

Answered by : charlesalexandre-roy

# Basic syntax:
cut -d "delimeter" -f split_number your_file
# Where:
#	- -d specifies the delimeter to split by
#	- -f specifies which element to return after splitting
# Note, if running this in a bash script, use syntax like:
"$(cut -d ',' -f split_number <<<$variable_to_split)"
# Note, there are lots of ways of doing this, e.g. with awk:
awk -F delimiter '{print $split_number}' your_file
# Where:
#	- -F specifies the field delimiter
# Note, awk also has a split function which has this syntax:
awk '{split($column, a, "delimiter"); action_after_split }' your_file
# Where:
#	- the column is the column to be split (awk uses whitespace to determine
#	columns by default)
#	- a is an array variable which will store the contents of the split
#	- delimiter is the delimiter by which to split the column
# Example usage:
# Say you have a file with this line:
my	file	with	fields_to_split
# You can print "to" with:
awk '{split($4, a, "_"); print a[2] }' your_file
--> to

Source : https://unix.stackexchange.com/questions/312280/split-string-by-delimiter-and-get-n-th-element/312400 | Last Update : Thu, 03 Mar 22

Question : separate words in lines bash

Answered by : armando-flores

Just use tr command for separating words output into separate lines:
tr -s '[[:punct:][:space:]]' '\n'
Example for
cat file.txt | tr -s '[[:punct:][:space:]]' '\n'

Source : | Last Update : Thu, 29 Oct 20

Answers related to bash split line and get element

Code Explorer Popular Question For Swift