How to get first four parts of the string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get first four parts of the string?
# 1  
Old 06-24-2018
How to get first four parts of the string?

I have the string:
Code:
XXXX.YYYY_ZZZ.20180724.01.txt

I need to get rid of .txt and get full four parts
Code:
XXXX.YYYY_ZZZ.20180724.01


I did:
Code:
CTL=`echo XXXX.YYYY_ZZZ.20180724.01.txt | rev | cut -d"." -f4 | rev`

But got only
Code:
YYYY_ZZZ


What should I do to get all four parts of that string?


Thanks for contribution
# 2  
Old 06-24-2018
Try
Code:
str=XXXX.YYYY_ZZZ.20180724.01.txt
CTL=${str%.*}



--
Note: this example is using parameter expansion
# 3  
Old 06-24-2018
It works, but I have a Korn shell. Are you sure it will work with Korn shell?
I believe I can use `awk -F"." {'print $5.$4.$3.$2'}`


str%. works because of
Code:
#!/bin/ksh

at the beginning of the script. Seems like it is csh.

Please advise


Thanks for contribution

Last edited by digioleg54; 06-24-2018 at 03:41 PM..
# 4  
Old 06-24-2018
Looking at your recent posts, it seems you should get your act together and find out what shell you REALLY use - and tell people in here reliably.



For your unsatisfying code in post#1, try a field range for cut:
Code:
echo XXXX.YYYY_ZZZ.20180724.01.txt | cut -d"." -f1-4
XXXX.YYYY_ZZZ.20180724.01

# 5  
Old 06-24-2018
Quote:
Originally Posted by digioleg54
It works, but I have a Korn shell. Are you sure it will work with Korn shell?
This works in the Bourne shell and any shell derived from it: ksh88, ksh93, bash. See the man page and llok for "parameter expansion" to find out more about these constructs.

Of course what Scrutinizer has suggested is the preferred way to do it because it doesn't need to spawn a new process. To show you that many other (not as resource-preserving but still working) ways exist: use the basename utility. If it is provided a second argument it interprets this as extension and eventually cuts off this from the end of a filename:

Code:
$ basename some_file.ext .ext
some_file

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

2. Shell Programming and Scripting

Converting parts of a string to "Hex"

Hi Guys, writing a small shell script, i need to convert parts of a string to "Hex". The problem is that it is not the full string that needs to be converted. I think it's best to show an example: $astring = "xxxxxx ABC+10+##########+DEF xxxx" This is only an example to show how the... (9 Replies)
Discussion started by: HansHansen
9 Replies

3. Shell Programming and Scripting

AWK splitting a string of equal parts of 500 chars

Hi , can someone help me how to make an AWK code for splitting a string of equal parts of 500 chars in while loop? Thank you! (4 Replies)
Discussion started by: sanantonio7777
4 Replies

4. Shell Programming and Scripting

perl, splitting out specific parts of the string

Hi there, I have an output from a command like this # ypcat -k netgroup.byuser| grep steven steven.* users_main,users_sysadmin,users_global,users_backup_team and wanted to pull the 'users' netgroups returned into a perl array, that will look like this users_main... (2 Replies)
Discussion started by: rethink
2 Replies

5. Shell Programming and Scripting

Spliting bash string into parts

Hello, let's say I have this string: string1="A\nB\nC D E\nFG\nH"; How can I split it so as to take every string separated with '\n' separately? For example, as for $string1, it would be split into string1_part1="A" string1_part2="B" string1_part3="C D E" string1_part4="FG"... (5 Replies)
Discussion started by: hakermania
5 Replies

6. UNIX for Dummies Questions & Answers

increment numbers in several parts of a string

I know how to do produce this: string01 string02 string03 several different ways. But how do I do produce this (without getting lost in recursion): string01morestring100yetmore10 string02morestring101yetmore20 string03morestring102yetmore30 ...... (2 Replies)
Discussion started by: uiop44
2 Replies

7. UNIX for Dummies Questions & Answers

How to cut a string in two parts and show the other part

hi everybody.. I have a string like : abcd:efgh xxyy:yyxx ssddf:kjlioi ghtyu:jkksk nhjkk:heuiiue please tell me how i can display only the characters after ":" in the output the output should be : efgh yyxx kjlioi jkksk heuiiue please give quick reply.. its urgent..!! (6 Replies)
Discussion started by: adityamitra
6 Replies

8. Shell Programming and Scripting

Truncate Specific Parts of a String

How do you truncate specific parts of a string. Example: 1 This is the string Goal: This is the string As you can see I'm trying to simply remove the first two characters of the string the number one and the space between the one and the word "this." Your help is appreciated. ... (8 Replies)
Discussion started by: royarellano
8 Replies

9. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies

10. Shell Programming and Scripting

A problem for sed? Remove parts of a string

Hi, My knowledge about sed is limited but I have a problem that I think can be solved with sed. I have a variable in a shell script that stores a lot of path/filenames and the delimitter between them is a space (they all exist on the same line). One part of the filename is the file creation... (4 Replies)
Discussion started by: pcrs
4 Replies
Login or Register to Ask a Question