read string value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read string value
# 1  
Old 09-06-2010
read string value

Hello All,
I have a sequence value like
2
2a
3
3a

when I execute this command
Code:
ls -1v *.sql | cat -n | grep 2 | awk ' { print $1 } '

it greps 2 and 3 which is 2 and 2a. I want the output to only display 3 ie, 2a.
Code:
[oracle@ccoshs02xvdbs02 deploy]$ ls -1v *.sql
1.taba.sql
2.tab2.sql
2a.ta.sql
3.view.sql
3a.se.sql
4.cons.sql

Thanks.

Last edited by pludi; 09-06-2010 at 02:33 PM.. Reason: code tags, please...
# 2  
Old 09-06-2010
I'm not sure I understand what your requirements are.

If you want to find all filenames that have two characters before the first dot (.), and grep through just those files, then something like this might be more suited:

Code:
ls -1v ??.* | xargs grep 2 | awk '{ print $1; }'

If you specifically need one character followed by 'a.' then use the file pattern ?a.* on the ls command.
# 3  
Old 09-06-2010
Quote:
Originally Posted by r_bastawala
[...]
it greps 2 and 3 which is 2 and 2a. I want the output to only display 3 ie, 2a.
Code:
[oracle@ccoshs02xvdbs02 deploy]$ ls -1v *.sql
1.taba.sql
2.tab2.sql
2a.ta.sql
3.view.sql
3a.se.sql
4.cons.sql

Thanks.
Do I understand that out that output you only want?
Code:
2a.ta.sql
3.view.sql

Correct?

Code:
ls -1v *.sql | awk '/2a\.|3\./ {print NR, $1}'

That shows in which line it was found, which it's the same job that cat -n was doing.
If you don't what to display in which line was found
Code:
ls -1v *.sql | awk '/2a\.|3\./'


Last edited by Aia; 09-06-2010 at 03:43 PM.. Reason: Adding the range
# 4  
Old 09-06-2010
like this
Code:
# ls -1v *.sql | grep -E '3[^a-z]|2a'
2a.ta.sql
3.view.sql

# 5  
Old 09-06-2010
If you want the most recent version of a particular sql file numbered according to your numbering system as sorted by "ls -1v", this is worth a try.

We use "ls" to cut the list down to those starting with "2." or "2[a-z]." then take the last in the list. The "2>/dev/null" allows for there to be no match.
The patterns got quite painful to avoid accidental matches with say "20a.table.sql" .

Code:
version_wanted="2"
ls -1v "${version_wanted}"\.*\.sql "${version_wanted}"[a-z]\.*\.sql 2>/dev/null| tail -1

2a.ta.sql


Last edited by methyl; 09-06-2010 at 06:02 PM.. Reason: paste errors
# 6  
Old 09-07-2010
Thanks Everyone
I have gone with methyl option.

Code:
[oracle@ccoshs02xvdbs01 deploy]$ ls -1v *.sql | cat -n
     1  1.tab1.sql
     2  2.tab1.sql
     3  2a.tabl1.sql
     4  3.tab1.sql
     5  1000.tab.sql

[oracle@ccoshs02xvdbs01 deploy]$ ls -1v "3"\.*\.sql  2>/dev/null| tail -1 | cat -n | awk  ' { print $1 } '
1
[oracle@ccoshs02xvdbs01 deploy]$ ls -1v "2"\.*\.sql  2>/dev/null| tail -1 | cat -n | awk  ' { print $1 } '
1
[oracle@ccoshs02xvdbs01 deploy]$ ls -1v "2a"\.*\.sql  2>/dev/null| tail -1 | cat -n | awk  ' { print $1 } '

With my list command when I gave 2
Code:
[oracle@ccoshs02xvdbs01 deploy]$ ls -1v *.sql | cat -n | grep 2 | awk ' { print $1 } '
2
3

Thanks.

---------- Post updated at 10:23 AM ---------- Previous update was at 04:50 AM ----------

Code:
[oracle@ccoshs02xvdbs01 deploy]$ ls -lv *.sql | cat -n
     1  -rw-r--r-- 1 oracle oinstall 36 Sep  6 13:50 1.tab1.sql
     2  -rw-r--r-- 1 oracle oinstall 39 Sep  6 13:51 2.tab1.sql
     3  -rw-r--r-- 1 oracle oinstall 40 Sep  6 13:51 2a.tabl1.sql
     4  -rw-r--r-- 1 oracle oinstall 39 Sep  6 13:52 3.tab1.sql
     5  -rw-r--r-- 1 oracle oinstall  0 Sep  7 10:29 1000.tab.sql
     6  -rw-r--r-- 1 oracle oinstall  0 Sep  7 15:26 1000a.tat.sql
     7  -rw-r--r-- 1 oracle oinstall  0 Sep  7 15:26 1001.tat.sql
     8  -rw-r--r-- 1 oracle oinstall  0 Sep  7 15:26 1002.tat.sql
     9  -rw-r--r-- 1 oracle oinstall  0 Sep  7 15:26 1003.tat.sql
    10  -rw-r--r-- 1 oracle oinstall  0 Sep  7 15:37 1004.tat.sql
    11  -rw-r--r-- 1 oracle oinstall  0 Sep  7 15:38 1004a.ta.sql
    12  -rw-r--r-- 1 oracle oinstall  0 Sep  7 15:38 1005.tab.sql

[oracle@ccoshs02xvdbs01 deploy]$ echo $INPUT_FILE
1004

[oracle@ccoshs02xvdbs01 deploy]$ ls -1v "${INPUT_FILE}"\.*\.sql "${INPUT_FILE}"[a-z]\.*\.sql 2>/dev/null| tail -1 | awk -F. ' { print $1 } '
1004a

I want 11 -- line number as the output
Any help please.

Last edited by Scott; 09-07-2010 at 01:13 PM.. Reason: Code tags, please...
# 7  
Old 09-07-2010
Code:
# INPUT_FILE=1004
# ls -lv *.sql | cat -n | grep ${INPUT_FILE}[^\.]* | sed "s/[[:blank:]]*\([0-9]*\) *.*\(${INPUT_FILE}[^\.]*\).*$/\1 -- \2/"
11 -- 1004a

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read file, and replace certain string with another string?

Hi all, the value in the following file is just an example. It could be a different value/network addresses. Here is my example of initial output in a file name net.txt Initial Output, net.txt The goal is to produce the following format which is to convert from CIDR to Netmask... (6 Replies)
Discussion started by: type8code0
6 Replies

2. Shell Programming and Scripting

Read text between two lines containing a string

Hi, I have text file like the following: Start a b 121 c d End Start a 31 e f End Start p o i k (5 Replies)
Discussion started by: ysrini
5 Replies

3. Shell Programming and Scripting

read string in a file

I'm writing a script for remote access. I wanted to put all my server's name on a file (server.txt) per line. and then the user will input the serversname and do ssh to it. sample. servers.txt server1 server2 server3 user will be asked to put what server he would like to ssh... (2 Replies)
Discussion started by: lhareigh890
2 Replies

4. Shell Programming and Scripting

Read Line and sent as variable for each string

Hi , I want to read below output, lets called it output1.txt and each string for every line will be declare as a variables. This is the input file 196 server_a server_unix_2 FW 196 server_b server_win_1 CD 196 server_c server_win_2 CD 196 server_bd ... (2 Replies)
Discussion started by: sQew
2 Replies

5. Shell Programming and Scripting

How read the part of the string into a variable?

Hi, I'm using bash and brand new to shell script. I would like to do the following. I have a string which is "UPDATE=1.0". I would like to read the value "1.0" alone in a variable. i.e the things afer "=" How do I do that? Thanks, (1 Reply)
Discussion started by: scriptfriend
1 Replies

6. Shell Programming and Scripting

read string from file into variable

Hello, I need to read from a file consisting of only one integer and enter that number into variable.can anyone help? the script is written in cshell. thanks. (4 Replies)
Discussion started by: offerbukra
4 Replies

7. Shell Programming and Scripting

read a value from a string/line

I have a file that contains the something like this a=15 b=21.5 c=544 and so on the question is how could I find the line that contains the value of c and store its value on a variable in bash shell?, Thanks in advanced (4 Replies)
Discussion started by: josegr
4 Replies

8. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

9. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

10. Shell Programming and Scripting

read -r string

Hi, can someone explain to me what does " read -r string" mean? i tried to locate the MAN pages of read, however, i can't seem to find any examples of using the -r flag. Would appreciate if anyone can enlighten me. (2 Replies)
Discussion started by: new2ss
2 Replies
Login or Register to Ask a Question