Print strings from a particular position in each line


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Print strings from a particular position in each line
# 1  
Old 11-17-2019
Print strings from a particular position in each line

I am using bash in Fedora 30

From the below lines (ls -l output), how can I print whatever is between the strings 'status_' and '.log'

Code:
$ ls -l | grep -i status 
-rw-rw-r--. 1 sysadmin sysadmin 378530 Nov 11 21:58 status_vsbm1.log
-rw-rw-r--. 1 sysadmin sysadmin 428776 Nov 11 21:58 status_OSMPRD1.log
-rw-rw-r--. 1 sysadmin sysadmin 393977 Nov 11 21:58 status_fxmdbp1.log
-rw-rw-r--. 1 sysadmin sysadmin 557476 Nov 11 21:58 status_cgsm1.log
-rw-rw-r--. 1 sysadmin sysadmin 555879 Nov 11 21:58 status_cgsm2.log
-rw-rw-r--. 1 sysadmin sysadmin 553381 Nov 11 21:58 status_cgsm3.log
-rw-rw-r--. 1 sysadmin sysadmin 556506 Nov 11 21:58 status_cgsm4.log
-rw-rw-r--. 1 sysadmin sysadmin 577325 Nov 11 21:58 status_cgsm5.log
-rw-rw-r--. 1 sysadmin sysadmin 415795 Nov 11 21:58 status_SOACAT1.log
-rw-rw-r--. 1 sysadmin sysadmin 449812 Nov 11 21:58 status_INTPHLK11.log
-rw-rw-r--. 1 sysadmin sysadmin 455842 Nov 11 21:58 status_INTPHLK21.log
-rw-rw-r--. 1 sysadmin sysadmin 447726 Nov 11 21:58 status_wmeaidbp1.log
-rw-rw-r--. 1 sysadmin sysadmin 385274 Nov 11 21:58 status_wmsmpdbp1.log
$

Expected output :

Code:
vsbm1
OSMPRD1
fxmdbp1
cgsm1
.
.
.

# 2  
Old 11-18-2019
there are probably a million ways to do it more elegantly but I would probably just try this
Code:
ls -ali | rev | cut -d' ' -f 1 | cut -c 1-7 | cut -c 5-99 | rev

# 3  
Old 11-18-2019
Something like this, also not very elegant:

Code:
ubuntu# egrep -o '_.*[0-9]' test.log | cut --complement -c  1
vsbm1
OSMPRD1
fxmdbp1
cgsm1
cgsm2
cgsm3
cgsm4
cgsm5
SOACAT1
INTPHLK11
INTPHLK21
wmeaidbp1
wmsmpdbp1

Or, in your case:

Code:
ls -l | grep -i status  | egrep -o '_.*[0-9]'  | cut --complement -c  1

There must be 100s of ways to do this which are much nicer ... LOL

Like ...

Code:
ls -l | egrep -o 'status_.*[0-9]' | cut --complement -c1-7

PS: kraljic .... You should really try yourself (post code) when asking a question like this. Show your work and what you tried yourself. You have over 100 posts here. Try yourself before asking, please.
This User Gave Thanks to Neo For This Post:
# 4  
Old 11-18-2019
Two stage process not using ls:
Code:
for f in status_*.log
do
   f1="${%*_}
   printf "%s\n" "${f1#.log}"
done

Which can be written as a single line:
Code:
for f in status_*.log; do f1="${%*_}; printf "%s\n" "${f1#.log}"; done

# 5  
Old 11-18-2019
Code:
ls | sed -n 's/^status_\(.*\)[.]log$/\1/p'

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 6  
Old 11-18-2019
Code:
ls | awk -F"status_|[.]log" ' { print $2 } '

This User Gave Thanks to anbu23 For This Post:
# 7  
Old 11-18-2019
Using external programs (such as cut, *grep,sed,awk or others) are a dire waste of precious resources as well as processes.


Quote:
Originally Posted by anbu23
[
Code:
ls | awk -F"status_|[.]log" ' { print $2 } '

Quote:
Originally Posted by zxmaus
Code:
ls -ali | rev | cut -d' ' -f 1 | cut -c 1-7 | cut -c 5-99 | rev



It is not clear what the two revs should accomplish, except making the line non-standard and GNU-dependant.



Quote:
Originally Posted by Neo
Code:
ubuntu# egrep -o '_.*[0-9]' test.log | cut --complement -c  1

Code:
ls -l | egrep -o 'status_.*[0-9]' | cut --complement -c1-7


This is using several non-standard GNU-specific extensions and will therefore only work with the GNU versions of the used utilities. It is dangerous to suggest such solutions, at least when foregoing a proper disclaimer that it won't work in most environments.


Quote:
Originally Posted by apmcd47
Code:
for f in status_*.log; do f1="${%*_}; printf "%s\n" "${f1#.log}"; done


This is - in principle - a valid solution although it seems the "#" and "%" operators have been confused, a double quote is missing and a few typos like
Code:
f1="${%*_}

will prevent proper execution. It is not necessary to use different variables for the expansion either:



Code:
for f in status_*.log; do f="${f%.log}"; printf "%s\n" "${f#status_}"; done

These 2 Users Gave Thanks to foad For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

2. Shell Programming and Scripting

Print the complete line based on position as input

Hi , I have a text file like below abcd2223232321212121324343 asdsfffhfgfgfhgfhghghghghghgh dfdfdfgfghfgfgfgfgfgghghghghgh dfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdf 1234567890sasasasfdfbffghffhf abcd222323232121212abcdsds in the above file i need to grep and print the lines if 14th to 18th... (4 Replies)
Discussion started by: aravindj80
4 Replies

3. Shell Programming and Scripting

How to print few strings in a line?

I have one script which gave final output as the below statment. Successfully added Nomination Petition Manifest to the Content Manager. Now i want a sed statement which will search for this statement in any given file and print only Nomination Petition Manifest. I don't want to hardcore... (13 Replies)
Discussion started by: Vikram_Tanwar12
13 Replies

4. UNIX for Dummies Questions & Answers

Help with finding matching position on strings

I have a DNA file like below and I am able to write a short program which finds/not an input motif, but I dont understand how I can include in the code to report which position the motif was found. Example I want to find the first or all "GAT" motifs and want the program to report which position... (12 Replies)
Discussion started by: pawannoel
12 Replies

5. Shell Programming and Scripting

grep a string and print strings on that line

i have a file like below ABS 12234 A C 12G CFY 23865 A C 34D i want to grep "A C" then print ABS 12G 12234 CFY 34D 23865 Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

6. Shell Programming and Scripting

Search in specific position and print the whole line

I have two files abc.dat and sant.dat (Big file 60k rows) for every line's 1,4 of abc.dat need to seach if this is present on 28,4 of sant.dat every line. if its present the output needs to go to bde.dat Example: contents abc.dat aaaa bbbb cccc dddd contents sant.dat this is... (4 Replies)
Discussion started by: ssantoshss
4 Replies

7. Shell Programming and Scripting

Search for multiple strings in specific position

Hi, I need to search for some strings in specific positions in a file. If the strings: "foo1", "foo2" or "foo3" is on position 266 or position 288 in a file i want the whole line printed. Any idea how to do it? (5 Replies)
Discussion started by: HugoH
5 Replies

8. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies

9. Shell Programming and Scripting

Finding character mismatch position in two strings

Hello, I would like to find an efficient way to compare a pair of strings that differ at one position, and return the difference and position. For example: String1 123456789 String2 123454789 returning something - position 6, 6/4 Thanks in advance, Mike (5 Replies)
Discussion started by: etherite
5 Replies

10. Shell Programming and Scripting

How to insert strings at certain position

Hi, I need to insert strings "0000 00" at the each line within the file. The postion is 37 to 42. ex. name1 name2 0000 00 nam name 0000 00 The "0000 00" in two lines should be lined up. I don't know why it's not lined up when I posted it. Can anyone help? (14 Replies)
Discussion started by: whatisthis
14 Replies
Login or Register to Ask a Question