Help with controlling string elements

 
Thread Tools Search this Thread
Operating Systems Linux Fedora Help with controlling string elements
# 1  
Old 03-13-2011
Help with controlling string elements

Hi All,

I have a general difficulty in understanding how to control single elements within a string. An example,



Code:
XYZ1234      ABCD5678

My expected output is :



Code:
ABCD1234     XYZ5678  (swapping subset of string elements of choice)
XYZ37            ACBD1214 (making calculations using string elements)

,etc

Could someone illuminate on such problems please.

Thanks in advance Smilie

PS: I'm a beginner to UNIX Image
# 2  
Old 03-13-2011
Read the following link about string operators and pattern matching operator :
String Operators (Learning the Korn Shell, 2nd Edition)

Then understand and reproduce the given examples.

Code:
$ a=/users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt

Code:
$ echo $a
/users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt

Code:
$ echo ${a#*/}
users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt

Code:
$ echo ${a##*/}
1900_CDCRM_CBF71_13022010_13022010.txt

Code:
$ echo ${a%_*}
/users/home/ctsgnb/1900_CDCRM_CBF71_13022010

Code:
$ echo ${a%%_*}
/users/home/ctsgnb/1900

Code:
$ echo ${a%.*}
/users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010

Code:
$ echo ${a#?}
users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt

Code:
$ echo ${a##?}
users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt

Code:
$ echo ${a%?}
/users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.tx

Code:
$ echo ${a%%?}
/users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.tx

---------- Post updated at 10:42 PM ---------- Previous update was at 10:23 PM ----------

You also have to understand meaning of meta character in regular expression see
Syntax of sed Commands (sed & awk, Second Edition)

---------- Post updated at 10:43 PM ---------- Previous update was at 10:42 PM ----------

some examples :
Code:
[ctsgnb@shell ~]$ echo $a
/users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/_/#/'
/users/home/ctsgnb/1900#CDCRM_CBF71_13022010_13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/_/#/g'
/users/home/ctsgnb/1900#CDCRM#CBF71#13022010#13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/_/#/2'
/users/home/ctsgnb/1900_CDCRM#CBF71_13022010_13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/_/#/3'
/users/home/ctsgnb/1900_CDCRM_CBF71#13022010_13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/ctsgnb/toto/'
/users/home/toto/1900_CDCRM_CBF71_13022010_13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/.*/# &/'
# /users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/\(.*\)ctsgnb/ctsgnb\1/'
ctsgnb/users/home//1900_CDCRM_CBF71_13022010_13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/.*\(ctsgnb\)/\1/'
ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt
[ctsgnb@shell ~]$

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 03-13-2011
Thank you very much ...

Its really exciting and your examples are helping me understand it nicely

If you think any other stuff is basic and will fit my level please let me know.

Thanks again and have a nice week ahead Smilie

---------- Post updated at 06:22 PM ---------- Previous update was at 06:13 PM ----------

Could you please comment on these last 3 sed command examples u sent,

didnt really get how they work !!

Cheers Smilie

Code:
[ctsgnb@shell ~]$ echo $a | sed 's/.*/# &/'
# /users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/\(.*\)ctsgnb/ctsgnb\1/'
ctsgnb/users/home//1900_CDCRM_CBF71_13022010_13022010.txt
[ctsgnb@shell ~]$ echo $a | sed 's/.*\(ctsgnb\)/\1/'
ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt

# 4  
Old 03-13-2011
Code:
ctsgnb@shell ~]$ echo $a | sed 's/.*/# &/'
# /users/home/ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt

replace all characters ('.' is any char, '*' is zero or more) with
a hash sign, space and whatever you captured ('&') as .*

Code:
[ctsgnb@shell ~]$ echo $a | sed 's/\(.*\)ctsgnb/ctsgnb\1/'
ctsgnb/users/home//1900_CDCRM_CBF71_13022010_13022010.txt

capture ('\(' and '\)' define the beg and end) any characters immediately before 'ctsgnb' string and
replace captured string and "ctsgnb" with "ctsgnb" concatenated with whatever was captured

Code:
[ctsgnb@shell ~]$ echo $a | sed 's/.*\(ctsgnb\)/\1/'
ctsgnb/1900_CDCRM_CBF71_13022010_13022010.txt

replace any chars before string "ctsgnb" and the string itself, with what was captured.
Only pattern that could match the capture is "ctsgnb" string.
Same as
Code:
echo $a | sed 's/.*ctsgnb/ctsgnb/'

without an instructional use of capturing with parentheses Smilie
# 5  
Old 03-14-2011
@mirni :

Yes, i used \( ...\) and \1 just for demonstration purpose.

@pawannoel :

when using
Code:
s/.../...&.../

the & refer to what has been matched in the first /.../ part (in red)

You could also play with the cut command and its options it may also be usefull in string manipulation in some case.
Code:
man cut

# 6  
Old 03-14-2011
@pawannoel:
This kind of substitution may come in handy also:
Code:
  $ var=kofoo23.txt
 $ echo ${var/foo/bar}
kobar23.txt

Can also be done with nested vars:
Code:
  $ str=BAR
  $ echo ${var/foo/$str}
koBAR23.txt

For details look at 'man bash' , and search for 'parameter expansion'

@ctsgnb:
Which shell (and version) are you using? I can't seem to find the
echo ${a##?}
echo ${a#?}
expansions in 'man bash' on my system (GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)), although it seems to work on CL. What do these do?
# 7  
Old 03-14-2011
That was ksh on a FreeBSD (not sure about the version, but it i think it was not a ksh93) the ? is expanded by the shell as matching any single character (but 1 and only 1) similar to (but not exactly same as) the dot in regex

---------- Post updated at 11:53 AM ---------- Previous update was at 11:27 AM ----------

@pawannoel :

still for demo purpose :

Code:
# echo "XYZ1234      ABCD5678"
XYZ1234      ABCD5678
# echo "XYZ1234      ABCD5678" | sed 's/^.../ABCD/;s/ABCD/XYZ/2'
ABCD1234      XYZ5678
# echo "XYZ1234      ABCD5678" | sed 's/^\(...\)\([^A-Z]*\)\(....\)\(.*\)$/\3\2\1\4/'
ABCD1234      XYZ5678
# echo "XYZ1234      ABCD5678" | sed 's/^\([A-Z]*\)\([0-9]*\)\([[:blank:]]*\)\([A-Z]*\)\([0-9]*\)$/\4\2\3\1\5/'
ABCD1234      XYZ5678
#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

"rhgb quiet" controlling the display of commands in single user mode ?"rhgb quiet" controlling the d

Why does removing "rhgb quiet" from the kernel boot parameters control whether or not the commands I enter are displayed in single user mode ? For instance, if I do not remove "rhgb quiet", when I am in single user mode, whatever command I type will not be displayed on the screen. The... (0 Replies)
Discussion started by: Hijanoqu
0 Replies

2. UNIX for Dummies Questions & Answers

Help with editing string elements

Hi All I have a question. I would like to edit some string characters by replacing with characters of choice located in another file. For example in sample file>S5_SK1.chr01 NNNNNNNNNNNNNNNNNNNCAGCATGCAATAAGGTGACATAGATATACCCACACACCACACCCTAACACTAACCCTAATCTAACCCTGGCCAACCTGTTT... (9 Replies)
Discussion started by: pawannoel
9 Replies

3. UNIX for Dummies Questions & Answers

Help with counting string elements

Hi All, I hv several files which have hundreds of lines each for example>XYZ.abc01 NNNTCGGTNNNNNCCACACACMYACACACCCACACCCACSCARCAC I'd like to exculde the first line beginning with ">" and then for the rest of the lines get a count for each string element. So for the above example I would like... (8 Replies)
Discussion started by: pawannoel
8 Replies

4. Shell Programming and Scripting

Array with String Elements

How can I get my array to understand the double-quotes I'm passing into it are to separate text strings and not part of an element? here's what I'm working with... db2 -v connect to foo db2 -x "select '\"' || stats_command || '\",' from db2law1.parallel_runstats where tabname = 'BAZ'" set... (4 Replies)
Discussion started by: djschmitt
4 Replies

5. Shell Programming and Scripting

Search array elements as file for a matching string

I would like to find a list of files in a directory less than 2 days old and put them into an array variable. And then search for each file in the array for a matching string say "Return-code= 0". If it matches, then display the array element with a message as "OK". Your help will be greatly... (1 Reply)
Discussion started by: mkbaral
1 Replies

6. Shell Programming and Scripting

ps: no controlling terminal

Any one know the below means : ps: no controlling terminal I had run a script in background : nohup ./benchmark.sh & and shutdown my windows system from where i connected through SSH I am using bash: The above script perfoms various tasks of Benchmarking Repositories Today the... (3 Replies)
Discussion started by: sriram003
3 Replies

7. Filesystems, Disks and Memory

Controlling I/O

Hi guys, Can anyone please tell me how I can control the I/O on my hardware devices in Suse Linux 8.1. I find that everytime I am reading a CD, or copying from a CD, I am unable to listen to music of watch a movie. Maybe this is intended to be like so, for the current high street technolgy... (1 Reply)
Discussion started by: bionicfysh
1 Replies

8. Programming

controlling terminal

What is controlling terminal in the case of daemon process? (2 Replies)
Discussion started by: Madhu Babu
2 Replies

9. UNIX for Dummies Questions & Answers

Controlling logfiles

I support an app that outputs alert and audit messages to one log file (vendor says they can't be separated). The script that I have written takes a copy (mv cmd) of the file to do the separation and reformatting. I have a problem that I loose records (messages are being written constantly, upto 3+... (5 Replies)
Discussion started by: nhatch
5 Replies

10. Shell Programming and Scripting

controlling screen display

How can I control the screen output when trying to read a large file onto the screen x number of lines at a time. I'm trying to use this is a bourne shell script. I want to display 10 lines of a file, pause the screen so that a user can read the file, and then display the next 10 lines of the file,... (6 Replies)
Discussion started by: jrdnoland1
6 Replies
Login or Register to Ask a Question