Sponsored Content
Operating Systems Linux Fedora Help with controlling string elements Post 302504108 by ctsgnb on Sunday 13th of March 2011 05:43:01 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Programming

controlling terminal

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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy