How to reform text string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to reform text string?
# 1  
Old 06-26-2015
How to reform text string?

Greetings,

How can I turn this:

Code:
t70c7n133 16 t70c7n134 16 t70c7n135 16 t70c7n136 16 t70c7n137 16 t70c7n138 16 t70c7n140 16 t70c7n141 16

into this:

Code:
t70c7n133 slots=16
t70c7n134 slots=16
t70c7n135 slots=16
t70c7n136 slots=16
t70c7n137 slots=16
t70c7n138 slots=16
t70c7n140 slots=16
t70c7n141 slots=16

I've come up with a couple ways but they're ugly and I've gotten close with sed but I'm not that good with it. So I'm sure there are much cleaner ways than what I've cooked up.

Thanks in advance.
# 2  
Old 06-26-2015
Show us what you've cooked up!
# 3  
Old 06-26-2015
For the purpose of simplicity:
Code:
xargs -n2 < file|awk '{$2="slots="$2}1'

In pure awk (will fail if there are trailing spaces in the file though Smilie):
Code:
awk 'BEGIN{RS=FS};ORS=(NR%2)?" slots=":"\n"{gsub(/\n *$/,"")}1'  file

# 4  
Old 06-26-2015
Thank-you pilnet101 for freely chiming in. And yes RudiC I did put forth some effort before posting.

The following variable is inherited:

Code:
HOST_LIST='t70c7n133 16 t70c7n134 16 t70c7n135 16 t70c7n136 16 t70c7n137 16 t70c7n138 16 t70c7n140 16 t70c7n141 16'

and I did this:
Code:
echo $HOST_LIST | fold -w 13 | sed  's/16/slots=16/'

However I realized that if the 16 was actually a single digit then the fold command is no good. Of course pilnet101's solution works well:
Code:
echo $HOST_LIST | awk 'BEGIN{RS=FS};ORS=(NR%2)?" slots=":"\n"{gsub(/\n *$/,"")}1'

Any other suggestions?

Last edited by rbatte1; 06-29-2015 at 09:04 AM.. Reason: Removed needless SIZE tags fopr clarity
# 5  
Old 06-27-2015
With any shell that uses basic Bourne shell syntax:
Code:
#!/bin/ksh
HOST_LIST='t70c7n133 16 t70c7n134 16 t70c7n135 16 t70c7n136 16 t70c7n137 16 t70c7n138 16 t70c7n140 16 t70c7n141 16'
set -- $HOST_LIST
while [ $# -ge 2 ]
do	printf '%s slots=%s\n' "$1" "$2"
	shift 2
done

And with a recent bash, a 1993 or later version of ksh, or another shell that supports indexed arrays and arithmetic commands:
Code:
#!/bin/ksh
HOST_LIST='t70c7n133 16 t70c7n134 16 t70c7n135 16 t70c7n136 16 t70c7n137 16 t70c7n138 16 t70c7n140 16 t70c7n141 16'
array=( $HOST_LIST )
for ((i = 0; i < ${#array[@]}; i += 2))
do	printf '%s slots=%s\n' "${array[i]}" "${array[i+1]}"
done

or with any POSIX-conforming shell that also supports indexed arrays as an extension to the standards:
Code:
#!/bin/ksh
HOST_LIST='t70c7n133 16 t70c7n134 16 t70c7n135 16 t70c7n136 16 t70c7n137 16 t70c7n138 16 t70c7n140 16 t70c7n141 16'
array=( $HOST_LIST )
i=0
while [ $i -lt ${#array[@]} ]
do	printf '%s slots=%s\n' "${array[i]}" "${array[i+1]}"
	i=$((i + 2))
done

All of which produce the output:
Code:
t70c7n133 slots=16
t70c7n134 slots=16
t70c7n135 slots=16
t70c7n136 slots=16
t70c7n137 slots=16
t70c7n138 slots=16
t70c7n140 slots=16
t70c7n141 slots=16

# 6  
Old 06-27-2015
As long as they are in pairs, you do not have to worry if the second member of the pair is a single digit or more or no digits at all.

Code:
echo $HOST_LIST | awk '{for(i=1;i<=NF;i+=2){printf "%s slots=%s\n", $i, $(i+1)}}'

# 7  
Old 06-27-2015
Looking at coming up with another solution for this in pure shell, similar to Don's solutions - I have been trying to use parameter expansion/substitution. I can only envisage that this would be achievable by doing this only if back references are supported. Looking at several sources online - ksh93 supposedly supports them, can anyone confirm this?

Something along the lines of:
Code:
tmp=${HOST_LIST// / strings=}
echo ${tmp//strings=\([![:digit:]]\)/\1"\n"}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

3. Shell Programming and Scripting

To Search for a string and to extract the string from the text

Hi Team I have an huge xml where i need to search for a ceratin numbers. For example 2014-05-06 15:15:41,498 INFO WebContainer : 10 CommonServicesLogs - CleansingTriggerService.invokeCleansingService Entered PUBSUB NOTIFY MESSAGE () - <?xml version="1.0" encoding="UTF-8"... (5 Replies)
Discussion started by: Kannannair
5 Replies

4. Shell Programming and Scripting

Using sed to get text between a string and next line after another string

Hi folks! I'm trying to get a part of a text inside a text file (sudoers actually) but I'm having trouble. Here is an example of a text: Cmnd_Alias DUMMY = /bin/ls /bin/car /usr/bin/whatever Cmnd_Alias TARGET = test test test test test \ ... (6 Replies)
Discussion started by: leandrorius
6 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

7. Shell Programming and Scripting

Reform Lines in File without blank lines and spaces

Hello All, I have a file with data as below. Each line consists of 21 fields. I am not able to load them back to the database. 50733339,"834","834 ","005010X279A1","N","Y","007977163","0001 ",30,"2110D ","EB ","EB007 ","2 ","Conditional Required Data Element Miss ing... (3 Replies)
Discussion started by: Praveenkulkarni
3 Replies

8. Shell Programming and Scripting

Search a string and append text after the string

Hi, I have a file like this... <o t="Batch" id="8410" p="/" g="32"> <a n="name"> <v s="DBBA1MM"/> </a> <a n="owner"> <v r="/Administrator"/> </a> <a n="rights"> <v s="95"/> </a> <a n="debugLevel"> <v s="3"/> </a> <a n="avsStoreLoc"> <v... (8 Replies)
Discussion started by: kesu2k
8 Replies

9. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question