1. Using awk:
awk -F “,” ‘{print $1, $2, $NF}’ <<< "string,to,split" 2. Using cut: cut -d "," -f 1,2,NF <<< "string,to,split" [ad_1]
If you are working with bash shell scripts, you might encounter situations where you need to split a string into different fields and extract certain fields from them. Fortunately, most shell scripts come with built-in tools that can help you achieve this task with ease. In this tutorial, you will learn how to split a string in shell and get the first, second, and last fields.
How to split a string in shell and get first, second and last field
By the following these steps, you can get first, second and last field in bash shell script from strings:
- Step 1: Define the string to be split
- Step 2: Split the string using delimiters
- Step 3: Extract the first, second, and last fields
- Step 4: Print the extracted fields
Step 1: Define the string to be split
Before you start splitting the string, you need to define the string that we want to split. In this guide, you will use the following string as an example:
string="Hello, world! How are you doing today?"
Step 2: Split the string using delimiters
Once you have defined the string, the next step is to split it into different fields using delimiters. In shell scripts, you can use the cut
command to split the string based on delimiters. The cut
command takes a delimiter as an argument and splits the string based on that delimiter. In our example, we will use the space character as the delimiter to split the string into different fields. Here’s the command to split the string:
fields=$(echo $string | cut -d' ' -f1,2,$(echo $string | awk '{print NF}') )
In this command, you are using the cut
command to split the string based on the space character delimiter. The -d
option specifies the delimiter to use, and the -f
option specifies the fields to extract. In our example, and want to extract the first field, second field, and last field. Then use awk
to get the number of fields in the string and use that as the last field to extract.
Step 3: Extract the first, second, and last fields
Now that you have split the string into different fields, you can extract the first, second, and last fields using the variable fields
. Here’s how to extract the fields:
first_field=$(echo $fields | cut -d' ' -f1) second_field=$(echo $fields | cut -d' ' -f2) last_field=$(echo $fields | cut -d' ' -f$(echo $fields | awk '{print NF}'))
In this command, you are using the cut
command to extract the first, second, and last fields from the fields
variable. The -d
option specifies the delimiter to use, and the -f
option specifies the field to extract. You use awk
again to get the number of fields in the fields
variable and use that as the last field to extract.
Step 4: Print the extracted fields
Finally, you can print the extracted fields to verify that the string has been split correctly. Here’s how to print the extracted fields:
echo "First field: $first_field" echo "Second field: $second_field" echo "Last field: $last_field"
This will print the extracted fields as follows:
First field: Hello, Second field: world! Last field: today?
Conclusion
In this tutorial, we have shown you how to split a string in shell and get the first, second, and last fields. By using the cut
and awk
commands, you can split any string into different fields based on delimiters and extract the fields that you need. Remember to test your script thoroughly to ensure that it works as expected.