Cut string from shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut string from shell
# 1  
Old 12-19-2018
Cut string from shell

How can I cut the date and leave it in 3 variable?

Code:
date="20181219"

year=substr(date,1,4)
monthsubstr(date,4,2)
daysubstr(date,6,2)
result

year=2018
month=12
day=19

this does not work for me
# 2  
Old 12-19-2018
Code:
date=20181219
year=${date:0:4}
month=${date:4:2}
day=${date:6:2}
echo $year $month $day

Notice that the first character of the string is position zero.
# 3  
Old 12-19-2018
Or, in case you run a recent shell, try


Code:
$ { read -n4 year; read -n2 month; read -n2 day; } <<< $date
$ echo $day $month $year
19 12 2018

This User Gave Thanks to RudiC For This Post:
# 4  
Old 12-19-2018
Longhand using OSX 10.14.1, default bash terminal calling dash, POSIX compliant.
This assumes that the format is consistent as per the OP.
Code:
Last login: Wed Dec 19 16:12:52 on ttys000
AMIGA:amiga~> dash
AMIGA:\u\w> # POSIX compliant using dash, OSX 10.14.1, default bash terminal.
AMIGA:\u\w> STRING="20181219"
AMIGA:\u\w> year="${STRING%????}"
AMIGA:\u\w> date="${STRING#??????}"
AMIGA:\u\w> STRING="${STRING#????}"
AMIGA:\u\w> month="${STRING%??}"
AMIGA:\u\w> echo $year $month $date
2018 12 19
AMIGA:\u\w> exit
AMIGA:amiga~> _

This User Gave Thanks to wisecracker 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

Cut the data with a string that has \ in it

Hi Team, Here's the record in a file. abc\USER DEFINED\123\345\adf\aq1 Delimiter here is "\USER DEFINED\" Expected output: abc|123\345\adf\aq1 Find "\USER DEFINED\" and replace it with "|". Can anyone please help us to fix this issue? Please use CODE tags as required by... (5 Replies)
Discussion started by: kmanivan82
5 Replies

2. UNIX for Dummies Questions & Answers

Cut only time from string

Hi Guys, I have following different lines separated by multiple spaces. And i need to cut only the time part (16:53) from the string. How can we achieve it? remedy 29210 1 1 07:55 ? 00:06:20 /opt/bmc/java/jre1.6.0_17/bin/java -Xms1024m -Xmx2048m -XX:MaxPermSize=1024m... (12 Replies)
Discussion started by: Khushbu
12 Replies

3. Shell Programming and Scripting

Cut the string

---------- Post updated at 10:31 AM ---------- Previous update was at 10:28 AM ---------- Hello, I am trying to get the string cut based on the following needs: String1=Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44"... (6 Replies)
Discussion started by: cartrider
6 Replies

4. Shell Programming and Scripting

Cut the string

Hi in a directory i've files having the following name for_category_info_19990101984301 for_catgry_meta_19991111214601 ------- I just want the name till year and month i.e; for_category_info_199901 for_catgry_meta_199911 How can i achieve the above string Thanks (2 Replies)
Discussion started by: smile689
2 Replies

5. Shell Programming and Scripting

How to cut a string from a variable

hello, i need to cut a string in unix but not from file ,, from variable, i searched on special line , and i need a specific data from this line , i get it on variable, how can i cut data that i need ??? merci (2 Replies)
Discussion started by: Reham.Donia
2 Replies

6. Shell Programming and Scripting

backward string cut

I need only the last .ko files to be stripped from the whole result., ie libiscsi2.ko, scsi_transport_iscsi2.ko etc.. kernel/drivers/scsi/libiscsi2.ko kernel/drivers/scsi/scsi_transport_iscsi2.ko kernel/drivers/scsi/scsi_transport_iscsi.ko kernel/fs/nls/nls_utf8.ko... (4 Replies)
Discussion started by: anilcliff
4 Replies

7. Shell Programming and Scripting

Scripting help- cut the string

Hi I am having a file with the following content(its created by ls of a directory): -rw-r----- 1 321 321 0 Oct 14 10:41 xcv23456 -rw-r----- 1 321 321 0 Oct 14 10:41 xcv23457 -rw-r----- 1 321 321 0 Oct 14 10:41 xcv23458 -rw-r----- 1 321 321 ... (6 Replies)
Discussion started by: digitalage
6 Replies

8. UNIX for Dummies Questions & Answers

how to cut prefix from a string

I have a file: chromosome1:436728 chromosome2:32892 ..... chromosome22:23781 I just want to get the number, not the prefix "chromosomeX", so I want to remove all the prefix ahead of the numbers. How can I do that?? Thanks!!! (PS: give me some very simple command so that I can understand... (4 Replies)
Discussion started by: kaixinsjtu
4 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. UNIX for Dummies Questions & Answers

Shell script: Cut / (slash) character in string

I have a string "\/scratch\/databases\". I want to have a new string "\/scratch\/databases" by cutting last '\' character using shell script. I can't do this Please help me. Thanks in advance ThuongTranVN (4 Replies)
Discussion started by: ThuongTranVN
4 Replies
Login or Register to Ask a Question