Trim after nth occurrence with loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trim after nth occurrence with loop
# 1  
Old 08-26-2018
Trim after nth occurrence with loop

Hello,
Below command trims right after the nth occurrence of a string.
When I try in while loop, it is not working.

In Terminal
Code:
IFS=/ ; read -ra val < Textfile ; echo "${val[*]:0:3}"

It gives only one line:
Code:
sunday/monday/tuesday

Textfile:
Code:
sunday/monday/tuesday/wednesday/thursday
august/feb/jan/april/
london/texas/paris/michigan/ontario

Expected output:
Code:
sunday/monday/tuesday
august/feb/jan
london/texas/paris

Script:
Code:
#!/bin/bash

while read -ra val
do
echo "${val[*]:0:3}"
done < Textfile

I could not solve this simple question.
Please let me know my fault.

Many thanks
Boris
# 2  
Old 08-26-2018
Try:
Code:
#!/bin/bash

oldIFS=$IFS
IFS=/
while read -ra val
do
  echo "${val[*]:0:3}"
done < Textfile
IFS=$oldIFS




--
Another way:
Code:
cut -d/ -f-3 Textfile


Last edited by Scrutinizer; 08-26-2018 at 07:19 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-26-2018
Dear Scrutinizer,
Many thanks, I am not familiar with field seperator command but both works nice.


Kind regards
Boris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace first occurrence of a string in while loop

####Solved#### Hello, My aim is to replace searched string with incremented value under ubuntu 16.04. Example: aasasasas 9030 31wwo weopwoep weerasas 9030 ew31wo ieopwoep bbqqqsas 9030 ew3swo ieeopwoep ccsaqpas 9030 ewiro o2opwoep Expected: aasasasas 9030 31wwo weopwoep weerasas 9031... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Substitute first occurrence of keyword if occurrence between two other keywords

Assume a string that contains one or multiple occurrences of three different keywords (abbreviated as "kw"). I would like to replace kw2 with some other string, say "qux". Specifically, I would like to replace that occurrence of kw2 that is the first one that is preceded by kw1 somewhere in the... (4 Replies)
Discussion started by: M Gruenstaeudl
4 Replies

3. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

4. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

5. Shell Programming and Scripting

Extract the text between the nth occurrence of square brackets

Please can someone help with this? I have a file with lines as follows: word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 When I use the... (7 Replies)
Discussion started by: Subhadeep_Sahu
7 Replies

6. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

7. Shell Programming and Scripting

How to output all lines following Nth occurrence of string

Greetings experts. Searched the forums (perhaps not hard enough?) - Am searching for a method to capture all output from a log file following the nth occurrence of a known string. Background: Using bash, I want to monitor my Oracle DB alert log file. The script will count the total # of... (2 Replies)
Discussion started by: cjtravis
2 Replies

8. Shell Programming and Scripting

Replacing nth occurrence

There is already one thread with the same heading. But here the case is little different. i have a line which have a field separator '|' abc|def|ghi|jkl|mno|pqr|stu|vwx|yz I want to replace every 3rd occurance + next character with the same + newline character.. I mean i want to enter a... (6 Replies)
Discussion started by: ratheeshjulk
6 Replies

9. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

10. UNIX for Dummies Questions & Answers

Finding nth occurrence in line and replacing it

Hi, I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db. Example: ... (5 Replies)
Discussion started by: stresing
5 Replies
Login or Register to Ask a Question