Linux / UNIX Tech Support Forum
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 ...
|
|||||||
| Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Sponsored Links | ||
|
|
|
||||
|
Try:
Code:
output=$(echo aa,bb,cc | cut -f2 -d,) echo $output
__________________
Vivek Gite Linux Evangelist |
|
|||
|
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 |
|
||||
|
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/' Code:
echo 'aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk' | sed 's/dd/text/g'
__________________
Vivek Gite Linux Evangelist |
|
|||
|
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 |
|
||||
|
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}'
Code:
echo 'rr,dd,aa,dd,dd,dd,ee,ff,gg,hh,ii,jj,kk' | awk -F',' '{$0=gensub(/dd/,"text",4);print}'
HTH
__________________
Vivek Gite Linux Evangelist |
|
|||
|
Quote:
|
| The Following User Says Thank You to cfajohnson For This Useful Post: | ||
nixcraft (19-05-2009)
| ||
|
||||
|
Wow! nicely done.
__________________
Vivek Gite Linux Evangelist |
|
|||
|
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 |
![]() |
| 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 | |
|
|
|
||||
| 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 |