nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

String manipulation

This is a discussion on String manipulation within the Shell scripting forums, part of the Development/Scripting category; I know how to get a filed, ie "echo aa,bb,cc | cut -f2 -d," BUt I do not know how ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 18-05-2009, 07:50 AM
Junior Member
User
 
Join Date: May 2009
OS: Debian
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Gideon is on a distinguished road
Default String manipulation

I know how to get a filed, ie "echo aa,bb,cc | cut -f2 -d,"

BUt I do not know how to SET a field

How do I easily and efficiently SET the 4th field in a stirng

ex How to I put the value "HERE" into the string "one,two,three,four,five,six,seven,eight,nine, ten" ?

Which command should I use and how do I do this ?

Thanks, in advance, Gideon
Reply With Quote
  #2 (permalink)  
Old 18-05-2009, 03:59 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Try:
Code:
output=$(echo aa,bb,cc | cut -f2 -d,)
echo $output
$output stores value for 2nd field.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 18-05-2009, 10:25 PM
Junior Member
User
 
Join Date: May 2009
OS: Debian
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Gideon is on a distinguished road
Default

Thanks, but I think I was not clear in my post

I am trying to set the 4th field within a comma deliminted string:

If I have the following line:

aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk

I want to place the value 'text' into the fourth field, as follows:

aa,bb,cc,text,ee,ff,gg,hh,ii,jj,kk

How can I do this ? Is there a paraleel command to cut ?

Thanks, Gideon
Reply With Quote
  #4 (permalink)  
Old 18-05-2009, 11:09 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Use sed to match first occurrence of 4th field called dd and replace with 'text':
Code:
echo 'aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk' | sed 's/dd/text/'
To find all occurrence of dd and replace with 'text':
Code:
echo 'aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk' | sed 's/dd/text/g'
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #5 (permalink)  
Old 19-05-2009, 01:00 AM
Junior Member
User
 
Join Date: May 2009
OS: Debian
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Gideon is on a distinguished road
Default

That is helpful, but unfortunately, I need this for a record with variable data, so I can not guarantee that the fourth field will actually be the first occurence of 'dd'.. Its possible I could have a record that looks like:

aa,dd,dd,dd,ee,ff,gg,hh,ii,jj,kk

Is there any way to set the fourth field.

How would you normally handle this in Korn Shell ?

Thanks, Gideon
Reply With Quote
  #6 (permalink)  
Old 19-05-2009, 01:38 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Bash / csh / ksh cannnot modify string. You need to use awk or sed or perl. Here is awk example to modify only 4th field,
Code:
echo 'aa,dd,dd,dd,ee,ff,gg,hh,ii,jj,kk' | awk -F',' '{$4="text"; print}'
Replaces only 4th instance of dd:
Code:
echo 'rr,dd,aa,dd,dd,dd,ee,ff,gg,hh,ii,jj,kk' | awk -F',' '{$0=gensub(/dd/,"text",4);print}'
All examples are tested on GNU awk (gawk).

HTH
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #7 (permalink)  
Old 19-05-2009, 03:10 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 82
Thanks: 0
Thanked 14 Times in 14 Posts
Rep Power: 2
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by Gideon View Post
That is helpful, but unfortunately, I need this for a record with variable data, so I can not guarantee that the fourth field will actually be the first occurence of 'dd'.. Its possible I could have a record that looks like:

aa,dd,dd,dd,ee,ff,gg,hh,ii,jj,kk

Is there any way to set the fourth field.

How would you normally handle this in Korn Shell ?

It can be done entirely in the shell with this function:
Code:
function repfield #@ Replace field in comma-separated string
{                 #@ USAGE: repfield string field replacement
  [ $# -ne 3 ] && return 1
  typeset string=$1 field=$2 replacement=$3 IFS=, x
  set -f
  x=( $string )
  set +f
  IFS=$' \t\n'
  x[field-1]=$replacement
  string=$(printf "%s," "${x[@]}")
  string=${string%,}
  printf "%s\n" "$string"
}
Reply With Quote
The Following User Says Thank You to cfajohnson For This Useful Post:
nixcraft (19-05-2009)
  #8 (permalink)  
Old 19-05-2009, 04:24 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
Originally Posted by cfajohnson View Post
It can be done entirely in the shell with this function:
Wow! nicely done.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #9 (permalink)  
Old 19-05-2009, 08:02 AM
Junior Member
User
 
Join Date: May 2009
OS: Debian
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Gideon is on a distinguished road
Default

Thanks, I really like that awk command

Just one thing. I tried this line:

echo 'aa,dd,dd,dd,ee,ff,gg,hh,ii,jj,kk' | awk -F',' '{$4="text"; print}'
and it returns the following output:

aa dd dd text ee ff gg hh ii jj kk

How can I put the comma's back into the rec ?

Thanks, Gideon
Reply With Quote
  #10 (permalink)  
Old 19-05-2009, 08:07 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 82
Thanks: 0
Thanked 14 Times in 14 Posts
Rep Power: 2
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Code:
echo 'aa,dd,dd,dd,ee,ff,gg,hh,ii,jj,kk' |
 awk -F',' 'BEGIN { OFS = ","} {$4="text"; print}'
Reply With Quote
Reply

Tags
awk , awk find and replace , awk gensub , sed , sed field , sed store output to variable


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
[Solved] date manipulation parmarjm Shell scripting 2 10-04-2009 12:56 AM
Shell Script for Searching a String vivekv Shell scripting 7 10-03-2009 03:18 AM
split files by specifying a string (bash shell) vikas027 Shell scripting 4 01-11-2007 04:22 PM
linux command search server for string chimu Linux software 2 26-07-2006 12:40 AM
example for string connect to a command. ryan Shell scripting 2 22-02-2005 01:05 PM


All times are GMT +5.5. The time now is 04:46 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38