Spliting bash string into parts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spliting bash string into parts
# 1  
Old 08-08-2011
Question Spliting bash string into parts

Hello, let's say I have this string:
Code:
string1="A\nB\nC D E\nFG\nH";

How can I split it so as to take every string separated with '\n' separately?
For example, as for $string1, it would be split into
string1_part1="A"
string1_part2="B"
string1_part3="C D E"
string1_part4="FG"
string1_part5="H"

i tried to do this way:
Code:
for i in $string1; do
   mpla mpla
done

but this way takes spaces as separators too, so that didn't work :/

Thanks in advance for any answers!Smilie
# 2  
Old 08-08-2011
Code:
#!/usr/bin/bash              
string1="A\nB\nC D E\nFG\nH";
echo $string1 >temp          
while read a                 
do                           
echo "$a"                    
done <temp

# 3  
Old 08-08-2011
Yes it's possible but I'm sure you don't need it.
Did your try your string in bash:
Code:
string1="A\nB\nC D E\nFG\nH"
echo "$string1"

I believe you need learn some basic stuff before. The semicolon at the end of the assignment says about this too.
# 4  
Old 08-08-2011
What about this:
Code:
#!/bin/bash

## Define a string
string1="A\nB\nC D E\nFG\nH"

## Replace the \n's with a single character, since IFS
## can only be 1 character.
match='\\n'
replacement='|'
string2=$(echo ${string1//${match}/${replacement}})

## Change the input field seperator to $replacement
IFS="$replacement"

## Parse the string based on the IFS.
set -- $string2

echo "1: $1"
echo "2: $2"
echo "3: $3"
echo "4: $4"
echo "5: $5"

Output:
Code:
$ test2
1: A
2: B
3: C D E
4: FG
5: H
$

# 5  
Old 08-08-2011
Simpler way for bash or newer ksh or even ash:

Code:
#!/bin/bash 
string1="A\nB\nC D E\nFG\nH"
OLDIFS="$IFS"
IFS="
"
        set -- `echo -e "$string1"`
IFS="${OLDIFS}"

echo $1
echo $2
echo $3
echo $4

In some shells you can remove the -e.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 08-08-2011
Last reply-best reply
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get first four parts of the string?

I have the string: XXXX.YYYY_ZZZ.20180724.01.txt I need to get rid of .txt and get full four parts XXXX.YYYY_ZZZ.20180724.01 I did: CTL=`echo XXXX.YYYY_ZZZ.20180724.01.txt | rev | cut -d"." -f4 | rev` But got only YYYY_ZZZ What should I do to get all four parts of that... (4 Replies)
Discussion started by: digioleg54
4 Replies

2. Shell Programming and Scripting

Issue Spliting String in Python

I have a string like below Note: I have have a single to any number of comma "," seperated string assigned to jdbc_trgt variable. I need to split jdbc_trgt using comma(,) as the delimiter. I tried the below but it fails as i dont know how can i read each split string iterately. for... (4 Replies)
Discussion started by: mohtashims
4 Replies

3. Shell Programming and Scripting

perl, splitting out specific parts of the string

Hi there, I have an output from a command like this # ypcat -k netgroup.byuser| grep steven steven.* users_main,users_sysadmin,users_global,users_backup_team and wanted to pull the 'users' netgroups returned into a perl array, that will look like this users_main... (2 Replies)
Discussion started by: rethink
2 Replies

4. UNIX for Dummies Questions & Answers

increment numbers in several parts of a string

I know how to do produce this: string01 string02 string03 several different ways. But how do I do produce this (without getting lost in recursion): string01morestring100yetmore10 string02morestring101yetmore20 string03morestring102yetmore30 ...... (2 Replies)
Discussion started by: uiop44
2 Replies

5. UNIX for Dummies Questions & Answers

How to cut a string in two parts and show the other part

hi everybody.. I have a string like : abcd:efgh xxyy:yyxx ssddf:kjlioi ghtyu:jkksk nhjkk:heuiiue please tell me how i can display only the characters after ":" in the output the output should be : efgh yyxx kjlioi jkksk heuiiue please give quick reply.. its urgent..!! (6 Replies)
Discussion started by: adityamitra
6 Replies

6. UNIX for Advanced & Expert Users

Bash shell: Cutting pasting only parts of the name of a directory into a variable

I have a script in a directory and want to search the directory before like follows: i=0 for file in ../HN_* do echo $file ((i+=1)) echo $i done Currently I get following output: ../HN_2 1 ../HN_3 2 (2 Replies)
Discussion started by: ABE2202
2 Replies

7. Shell Programming and Scripting

Truncate Specific Parts of a String

How do you truncate specific parts of a string. Example: 1 This is the string Goal: This is the string As you can see I'm trying to simply remove the first two characters of the string the number one and the space between the one and the word "this." Your help is appreciated. ... (8 Replies)
Discussion started by: royarellano
8 Replies

8. Shell Programming and Scripting

awk help with string spliting

Hello all, I am having a problem with awk's string split function. I have a string that has a number at the end, I am trying to remove the alpha portion of the string and just have the numeric part. Here is my code and the result: BEGIN { word = "$category121"; split(word, a, 121) print... (2 Replies)
Discussion started by: RobertSubnet
2 Replies

9. Shell Programming and Scripting

A problem for sed? Remove parts of a string

Hi, My knowledge about sed is limited but I have a problem that I think can be solved with sed. I have a variable in a shell script that stores a lot of path/filenames and the delimitter between them is a space (they all exist on the same line). One part of the filename is the file creation... (4 Replies)
Discussion started by: pcrs
4 Replies

10. Shell Programming and Scripting

With Regex Spliting the string into Alphanumeric and Numeric part

Hi there With shell script I'm trying to split the string into two parts. One is alphanumeric part, the other one is a numeric part. dummy_postcode_1 = 'SL1' --> res_alpha = 'SL' and res_numeric = '1' dummy_postcode_2 = 'S053' --> res_alpha = 'S' and res_numeric = '053' ... (1 Reply)
Discussion started by: ozgurgul
1 Replies
Login or Register to Ask a Question