How to reform text string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to reform text string?
# 8  
Old 06-27-2015
Quote:
Originally Posted by pilnet101
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"}

With the above code, bash version:
Code:
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)

and ksh version:
Code:
Copyright (C) 2007 Free Software Foundation, Inc.
  version         sh (AT&T Research) 93u+ 2012-08-01

produce the output:
Code:
t70c7n133 strings=16 strings=t70c7n134 strings=16 strings=t70c7n135 strings=16 strings=t70c7n136 strings=16 strings=t70c7n137 strings=16 strings=t70c7n138 strings=16 strings=t70c7n140 strings=16 strings=t70c7n141 strings=16

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 06-27-2015
sed solution based on you own approach, leaving out fold:
Code:
echo $HOST_LIST | sed -r 's/ ([0-9]+)( |$)/ slots=\1\n/g;s/\n$//'

This User Gave Thanks to RudiC For This Post:
# 10  
Old 06-27-2015
Code:
$ printf "%s slots=%s\n" $HOST_LIST
t70c7n133 slots=16
t70c7n134 slots=16
t70c7n135 slots=16
t70c7n136 slots=16
t70c7n137 slots=16
t70c7n138 slots=16
t70c7n140 slots=16
t70c7n141 slots=16

These 5 Users Gave Thanks to Aia For This Post:
# 11  
Old 06-27-2015
Longhand using OSX 10.7.5, default terminal using sh...
Code:
#!/bin/sh
# reformat
HOST_LIST='t70c7n133 16 t70c7n134 16 t70c7n135 16 t70c7n136 16 t70c7n137 16 t70c7n138 16 t70c7n140 16 t70c7n141 16'
# Working section.
HOST_LIST=( $HOST_LIST )
for (( n=0; n < ${#HOST_LIST[@]}; n+=2 ))
do
	echo "${HOST_LIST[$n]} slots=${HOST_LIST[$((n+1))]}"
done > /tmp/text
# End of working section.
HOST_LIST=$( cat /tmp/text )
echo "$HOST_LIST"

Results:-
Code:
Last login: Sat Jun 27 08:18:39 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./reformat
t70c7n133 slots=16
t70c7n134 slots=16
t70c7n135 slots=16
t70c7n136 slots=16
t70c7n137 slots=16
t70c7n138 slots=16
t70c7n140 slots=16
t70c7n141 slots=16
AMIGA:barrywalker~/Desktop/Code/Shell> _

# 12  
Old 06-29-2015
Thank-you all for the variety of solutions.
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