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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to cut a string in two parts and show the other part
# 1  
Old 05-26-2010
Question 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..!!
thanx..
# 2  
Old 05-26-2010
Code:
cut -d":" -f2 file

Code:
sed "s/.*://" file

Code:
awk -F":" '  {print $2} ' file

# 3  
Old 05-26-2010
Code:
while IFS=: read dump field; do echo $field; done < input.txt
cut -d: -f2 input.txt
sed -e 's/^.*://' input.txt
awk -F: '{print $2}' input.txt

# 4  
Old 05-26-2010
Code:
perl -F: -ane 'print $F[1]' infile

# 5  
Old 05-27-2010
Quote:
Originally Posted by pseudocoder
Code:
perl -F: -ane 'print $F[1]' infile

I am using this here as follows
Code:
perl -F: -ane 'print $F[0]'  < /etc/passwd | sort | xargs echo

but due to absence of newline char in between the results, I am not getting the output as expected. If I use '\n' inside print (in single quotes) error is throwing up. If I surround the print field in double quotes I am getting weird output. How to solve it?

Last edited by royalibrahim; 05-28-2010 at 06:13 AM..
# 6  
Old 05-27-2010
Change
Code:
perl -F: -ane 'print $F[0]'  < /etc/passwd | sort | xargs echo

to
Code:
perl -F: -alne 'print $F[0]'  < /etc/passwd | sort | xargs echo

The -l switch will automatically remove newlines from input and add it to output.
# 7  
Old 05-27-2010
resultingstring=$(echo $string | cut -d":" -f2)


say string="sumthing:nothing"

resultingstring will have "nothing".
 
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 get first four parts of the string?

I have the string: XXXX.YYYY_ZZZ.20180724.01.txt I need to get rid of .txt and get full four parts XXXX.YYYY_ZZZ.20180724.01 I did: CTL=`echo XXXX.YYYY_ZZZ.20180724.01.txt | rev | cut -d"." -f4 | rev` But got only YYYY_ZZZ What should I do to get all four parts of that... (4 Replies)
Discussion started by: digioleg54
4 Replies

2. Shell Programming and Scripting

Parsing a log file to cut off some parts

Dear all, I would like to use SQL's log file to extract information from it. This file can include four different types of instruction with the number of lines involved for each of them: -> (1) "INSERT" instruction with the number of lines inserted -> (2) "UPDATE" instruction with the... (4 Replies)
Discussion started by: dae
4 Replies

3. UNIX for Dummies Questions & Answers

How to cut part of a string in reverse?

Hi, how to cut part of a string sing delimiter in reverse input file 1,2,st-pa-tr-01,2,3,4, 2,3,ff-ht-05,6,7,8 how can i obtain strings till st-pa-tr ff-ht i.e cutting the last part og string -01 and -05 Thanks & Regards Nivi edit by bakunin: changed thread title (typo) (3 Replies)
Discussion started by: nivI
3 Replies

4. Shell Programming and Scripting

Cut the path into two parts

Hi, file=/usr/lib I need to cut and put it into two variable like string1=/usr string2=lib I made it for string2 string2=${file#/*/} How to get String1 in the same way which I have get string2. Use even more code tags ;) (4 Replies)
Discussion started by: munna_dude
4 Replies

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

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

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

8. UNIX for Dummies Questions & Answers

cut and print part of a string

I have a file that contains: yahoo.com.23456 web.log.common.us.gov.8675 192.168.1.55.34443 john-doe.about.com.22233 64.222.3.4.120 sunny.ca.4442 how can i remove the strings after the last dot (.) and reprint the file? Thanks. (3 Replies)
Discussion started by: apalex
3 Replies

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

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