Find the position of a pattern on a line from a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the position of a pattern on a line from a csv file
# 1  
Old 03-04-2011
Find the position of a pattern on a line from a csv file

hello I'm doing a unix program and i'm using many file csv.in each csv file the colums are separated by ";" I would like to know the position of a pattern. For example for a line yyyy, bbbb, cccc; ddddd;eeee. I will like for example by finding the position of the pattern "cccc" and the response is 3. Thank you in advance for your help.
# 2  
Old 03-04-2011
What is your system? What is your shell?
# 3  
Old 03-04-2011
unix. I'm programming in shell unix
# 4  
Old 03-04-2011
There's no actual software product called "UNIX" that you can go and get, then install on a computer. There hasn't been for a very long time. UNIX is a blueprint now, a standard around which operating systems are designed.

UNIX-based systems often have a variety of shells to choose from anyway, varying in features, so we also need to know which one you're using.

Try uname and uname -a to find out what you're actually running.

Try echo $SHELL to find out what your shell actually is.

Last edited by Corona688; 03-04-2011 at 05:59 PM..
# 5  
Old 03-04-2011
uname ---> Linux
uname -

---------- Post updated at 10:59 PM ---------- Previous update was at 10:57 PM ----------

uname -a Linux papis-laptop 2.6.32-24-generic #39-Ubuntu SMP Wed Jul 28 06:07:29 UTC 2010 i686 GNU/Linux
echo $SHELL --->/bin/bash

Thank u
# 6  
Old 03-04-2011
There we go. Thanks. Smilie

I'm assuming you meant "yyyy;bbbb;cccc;ddddd;eeee" because its position wouldn't be 3 otherwise...

Code:
#!/bin/bash
STR="cccc"
LINE=1
while IFS=";" read -a ARR
do
        for ((N=0; N<"${#ARR[@]}"; N++))
        do
                [[ "$STR" == "${ARR[$N]}" ]] && echo "line $LINE: $((N+1))"
        done
        ((LINE++))
done < filename.csv

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 03-04-2011
thank u very much. it's work. you save my life Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find all the multi line pattern and redirecting it to a file?

I've a file like this {multi line ....... ....... pattern} { some other stuff ......... } {multi line ....... ....... pattern} { some other stuff ......... } and so on (2 Replies)
Discussion started by: aamir_raihan
2 Replies

2. Shell Programming and Scripting

Need help on find and replacement on specific line and position

I have a file with 100 lines. On 50 th line , from position 5 to rest of the data , I need to change the occurrence of A to B and Occurrence of M to N. Input file : Line1 Line2 Line3 -- -- 12345ABCDEFM --- -- Line 100 Output Line1 Line2 (40 Replies)
Discussion started by: Rajesh_us
40 Replies

3. UNIX for Dummies Questions & Answers

Find next line based on pattern, if it is similar pattern skip it

Hi, I am able to get next line if it is matching a particular pattern. But i need a way to skip if next line also matches same pattern.. For example: No Records No Records Records found got it Records found Now i want to find 'Records found' after 'No Records' pattern matches.. ... (5 Replies)
Discussion started by: nagpa531
5 Replies

4. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

5. UNIX for Dummies Questions & Answers

find the file names having specified pattern at specified position in the current directory

I would need a command for finding first 15000 of the file names whose 25th postion is 5 in the current directory alone. I do have this painful command find . -name '5*' | head -15000 | cut -c3- please refine this. Of course the above command also searches in the sub directories... (3 Replies)
Discussion started by: vk39221
3 Replies

6. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

7. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

8. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

9. Shell Programming and Scripting

Remove/Find files based on position pattern

Hi All, Please help me to find or remove files based on position based search pattern. file1.txt: aaabbbccc dddeeefff iiijjjkkk file2.txt: lllmmmnnn ooopppqqq rrrsssttt file3.txt: uuuvvvwww xxxeeeyyy zzzcccooo From the above files, I like to delete the files that have "eee"... (1 Reply)
Discussion started by: kumarn
1 Replies
Login or Register to Ask a Question