Problem with traverse through line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem with traverse through line
# 1  
Old 02-12-2014
Problem with traverse through line

i have a file like below

New_file.txt
Code:
123|345|ab cd|ef gh
345|456|jk hu|uyh

My script is

Code:
#!/bin/ksh
set -x
for line in `cat New_file.txt`
do
a1=`echo $line|cut -d '|' -f1`
echo $a1
done


output

Code:
++ cat New_file.txt
+ for line in '`cat New_file.txt`'
++ echo '123|345|ab'
++ cut -d '|' -f1
+ a1=123
+ echo 123
123
+ for line in '`cat New_file.txt`'
++ echo 'cd|ef'
++ cut -d '|' -f1
+ a1=cd
+ echo cd
cd
+ for line in '`cat New_file.txt`'
++ echo gh
++ cut -d '|' -f1
+ a1=gh
+ echo gh
gh
+ for line in '`cat New_file.txt`'
++ echo '345|456|jk'
++ cut -d '|' -f1
+ a1=345
+ echo 345
345
+ for line in '`cat New_file.txt`'
++ echo 'hu|uyh'
++ cut -d '|' -f1
+ a1=hu
+ echo hu
hu


if my line is

Code:
123|345|ab cd|ef gh

$line only reads below as a first line

Code:
123|345|ab

Code:
cd|ef

as second line

Code:
gh

as third line

and go on

my need is All first line should be considered as one line and my $a should be 123 and 345

Last edited by Scrutinizer; 02-12-2014 at 02:57 AM.. Reason: extra code tags
# 2  
Old 02-12-2014
Try :

Code:
[akshay@aix tmp]$ cat file
123|345|ab cd|ef gh
345|456|jk hu|uyh

Code:
[akshay@aix tmp]$ awk '{for(i=1;i<=NF;i++)printf "%s%s",$i,(i == 3 || i == 5 || i == 6 )? RS : OFS}' FS='[| ]' OFS='|' file
123|345|ab
cd|ef
gh
345|456|jk
hu|uyh

# 3  
Old 02-12-2014
Us a while read loop instead of a for loop:

Code:
while read line
do
  ...
done < New_file.txt

Also quote variable references: echo "$line"

--
You can also feed the output of cut into a while loop (using a pipe symbol (|)), which would be more efficient:
Code:
cut -d '|' -f1 New_file.txt |
while read a1
do
  echo "$a1"
done

You can also use IFS with read to separate instead of cut, which would be even more efficient:
Code:
while IFS=\| read a1 _
do
  echo "$a1"
done < New_file.txt

Here _ is a dummy variable that catches the rest of the line after the first |

Last edited by Scrutinizer; 02-12-2014 at 03:11 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Traverse through directories

Hi all, Require help in completing a shell script. i do have a total of 90 directories where in we have different sub-directories and one of the sub directory named logs I need to go inside the logs subdirectory and check if a particular log is present or not. for example below is the... (3 Replies)
Discussion started by: bhaskar t
3 Replies

2. Shell Programming and Scripting

Traverse Latest directory First

I wish to traverse the latest to the oldest directory based on its timestamp. ls -ltr drwxr-x--- 3 admin bel 1024 Jan 22 02:29 sys drwxr-x--- 2 admin bel 2048 Jan 22 02:30 admin drwxr-x--- 10 admin bel 24576 Jan 23 21:31 bin For the above i need to cd first to... (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

4. Shell Programming and Scripting

Traverse through directory....

hi I have a directory structure like Parent Parent/child1/ Parent/child2/ Parent/child3/ and the each main directory contains Parent/child1/file1.txt, Parent/child1/fil2.zip ....... Parent/child2/file1.txt,Parent/child/fil2.zip ...... Now i want to traverse to each and want to... (1 Reply)
Discussion started by: Reddy482
1 Replies

5. Shell Programming and Scripting

find in given path do not want to traverse to its sub-directories

Hi All, My UNIX Version is: OS Name Release Version AIX appma538 3 5 I want to find certain files with some criterias under the given path. At the same time i want to find the files which resides under the given directory, but normal find traverse to its sub-directories... (4 Replies)
Discussion started by: Arunprasad
4 Replies

6. Shell Programming and Scripting

Traverse a flatfile and check for errors

Hi, I need to check a flatfile for various parameters like length of the record, format of record, any tab character present in the record etc., for checking presence of tab character, i'm trying to use the following code and i'm not sure if the same is right. Pls Help. nawk '{print... (1 Reply)
Discussion started by: aravindc
1 Replies

7. UNIX for Dummies Questions & Answers

Using File Descriptors, traverse a list

I have written this code, and according to my research it SHOULD be going down the list until it is finished, but I am getting blank feedback. Nothing is being output as far as I can tell. #!/bin/sh while echo Enter to start traversing read enter do read list <&3 echo $list done any... (2 Replies)
Discussion started by: MaestroRage
2 Replies

8. Shell Programming and Scripting

Traverse catalogs

Here is my problem (it seems I've a lot of problems nowadays). I have several folders: runner.20070830.12.45.12 runner.20070830.12.45.15 runner.20070830.12.45.17 runner.20070830.12.45.20 runner.20070830.12.45.45 runner.20070830.12.45.55 Each catalog contains some html-files. I... (3 Replies)
Discussion started by: baghera
3 Replies

9. Shell Programming and Scripting

Traverse Directory Tree for backup

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub directory in source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (1 Reply)
Discussion started by: srmadab
1 Replies

10. UNIX for Dummies Questions & Answers

Fastest way to traverse through large directories

Hi! I have thousands of sub-directories, and hundreds of thousands of files in them. What is the fast way to find out which files are older than a certain date? Is the "find" command the fastest? Or is there some other way? Right now I have a C script that traverses through and checks... (5 Replies)
Discussion started by: sreedharange
5 Replies
Login or Register to Ask a Question