printing patterns

I have the following file :

1 2 3 4 5
6 7 8 9 0
9 8 7 6 5
4 3 2 1 0

0 1 2 3 4

Write scripts to
get the following outputs :

A)

1
7
7
1
4

B)

1 2 3 4 5
6 7 8 9
9 8 7
4 3
0

please give a solution...

Sounds like homework... any trials?...

a clue!...

root@bt:/tmp# awk '...' infile
1 2 3 4 5
6 7 8 9
9 8 7
4 3
0
root@bt:/tmp# awk '...' infile
1
7
7
1
4

--ahamed

please try using arrays or search and cut options

---------- Post updated at 12:18 AM ---------- Previous update was at 12:17 AM ----------

use awk

this ids my idea i got the result
ans B)
#!/bin/bash
cat p | sed -n 1p |cut -d " " -f1-5
cat p | sed -n 2p |cut -d " " -f1-4
cat p | sed -n 3p |cut -d " " -f1-3
cat p | sed -n 4p |cut -d " " -f1-2
cat p | sed -n 6p |cut -d " " -f1
ans A)
#!/bin/bash

for (( i=1; i<=4; i++ ))
do
cat p | sed -n $i\p | cut -d " " -f $i
done
cat p | sed -n 6p | cut -d " " -f 5

can anyone do the ist one using "for" loop.... give solution

i=1;while read x;do echo $x | cut -d" " -f"$i";i=$(($i + 1));done < p

---------- Post updated at 14:27 ---------- Previous update was at 14:23 ----------

awk 'BEGIN{i=0}++i{print $i}' p

If this is homework, your prof is never gonna believe you wrote these one-liners.

one more .. Just a try .. :wink:

$ nawk '{print "echo \""$0"\"|cut -d\" \" -f"NR } ' infile | sh

Another one...

awk '{NF-=i++}1' infile

1 2 3 4 5
6 7 8 9
9 8 7
4 3
0
awk '{print $++i}' infile

1
7
7
1
4

--ahamed

 
$ grep -v "^$" test | nawk '{a=NR; print $a}'
1
7
7
1
4

 
$ grep -v "^$" test | nawk '{for(i=1;i<=NF-a;i++){printf("%s ",$i);if(i==NF-a)printf("\n")}a+=1;}'                                                 
1 2 3 4 5 
6 7 8 9 
9 8 7 
4 3 
0 

The conventional way...

#!/bin/bash
 
i=0
while read line
do
  arr[$i]=$line
  ((i=i+1))
done < infile
 
echo "Problem : 1"
j=0;c=$i
while [ $j -lt $i ]
do
  echo ${arr[$j]} | cut -f1-$c -d" "
  ((j=j+1))
  ((c=c-1))
done
 
echo "Problem : 2"
j=0;c=1
while [ $j -lt $i ]
do
  echo ${arr[$j]} | cut -f$c -d" "
  ((j=j+1))
  ((c=c+1))
done

--ahamed

The Perl way :wink:

perl -lane 'print $F[$i++]' infile

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.