How to read the user inputted file name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read the user inputted file name?
# 8  
Old 03-26-2012
Quote:
Originally Posted by rac
thnx .....
but still...in general if i want to store the values of variables in a log file, how can i do it..?
My desired output in the log file

-----------------------------------
Line num finalcount
2 3
3 4
4 4
. .
. .
. .
. . and so on
what is your input file?
is like that?
Code:
# cat 1
1 2 3 4 5
6 7 8 9 0 11
9 8 7 6 5 4 3
4 3 2 1 0 -1
0 1 2 3

Code:
# awk 'BEGIN{printf "%s%5s\n","Line","Nr fcount"}{gsub(" "," } ",$0);printf "%s%10s\n",NR,NF}' 1
LineNr fcount
1         9
2        11
3        13
4        11
5        8

This User Gave Thanks to ygemici For This Post:
# 9  
Old 03-26-2012
My input file

My input file will something like this--> say test.txt

text.txt
-----------------------
xyz --> I do not want to consider this line (1st)
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
123 --> again this line will not be considered as this is the last line

so output (as per my code) comes to
1
2
3
4
5
6
7
which is correct.
Now, i want to redirect this ouput to a file(maybe a text file) which should have the line number and the the output.
thnx a ton in advanceSmilie

---------- Post updated at 07:33 AM ---------- Previous update was at 05:39 AM ----------

this file contains records...one record is 570 characters..
thought the output is showing only 288 !!! Smilie plz help Smilie
# 10  
Old 03-26-2012
Quote:
Originally Posted by rac
My input file will something like this--> say test.txt

text.txt
-----------------------
xyz --> I do not want to consider this line (1st)
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
123 --> again this line will not be considered as this is the last line

so output (as per my code) comes to
1
2
3
4
5
6
7
which is correct.
Now, i want to redirect this ouput to a file(maybe a text file) which should have the line number and the the output.
thnx a ton in advanceSmilie

---------- Post updated at 07:33 AM ---------- Previous update was at 05:39 AM ----------

this file contains records...one record is 570 characters..
thought the output is showing only 288 !!! Smilie plz help Smilie
Code:
# cat file
xyz
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
123

Code:
# sed '1d;$d' file|awk 'BEGIN{printf "%s%10s\n","LNR","fcount"}{printf "%s%10s\n",NR+1,length($0)}' >newfile
LNR    fcount
2         1
3         2
4         3
5         4
6         5
7         6
8         7
9         8

Code:
# i=2;printf "%s%10s\n" "LNR" "fcount";while read -r l;do printf "%-10d" $i;echo "$l"|sed 's/.//'|wc -c;((i++));done< <(sed '1d;$d' file) >newfile
LNR    fcount
2         1
3         2
4         3
5         4
6         5
7         6
8         7
9         8


regards
ygemici

Last edited by ygemici; 03-26-2012 at 03:07 PM..
This User Gave Thanks to ygemici For This Post:
# 11  
Old 03-27-2012
[QUOTE=ygemici;302612983]
Code:
# cat file
xyz
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
123

Code:
# sed '1d;$d' file|awk 'BEGIN{printf "%s%10s\n","LNR","fcount"}{printf "%s%10s\n",NR+1,length($0)}' >newfile
LNR    fcount
2         1
3         2
4         3
5         4
6         5
7         6
8         7
9         8



I used this code and it worked wonders !! cheers !! now i am willing to optimise this by say if the fcount is not 7, then it should say, wrong number of cout.
LNR fcount fcount(new)
2 1 wrong number fo count
3 2 wrong number fo count
4 3 wrong number fo count
5 4 wrong number fo count
6 5 wrong number fo count
7 6 wrong number fo count
8 7
9 8 wrong number fo count
# 12  
Old 03-27-2012
[QUOTE=rac;302613251]
Quote:
Originally Posted by ygemici
Code:
# cat file
xyz
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
123

Code:
# sed '1d;$d' file|awk 'BEGIN{printf "%s%10s\n","LNR","fcount"}
{printf "%s%10s\n",NR+1,length($0)}' >newfile
LNR    fcount
2         1
3         2
4         3
5         4
6         5
7         6
8         7
9         8



I used this code and it worked wonders !! cheers !! now i am willing to optimise this by say if the fcount is not 7, then it should say, wrong number of cout.
LNR fcount fcount(new)
2 1 wrong number fo count
3 2 wrong number fo count
4 3 wrong number fo count
5 4 wrong number fo count
6 5 wrong number fo count
7 6 wrong number fo count
8 7
9 8 wrong number fo count
Code:
# sed '1d;$d' file|awk 'BEGIN{printf "%10s%15s%15s\n","LNR","fcount","fcount(new)"}
{if(length($0)!=7)w="wrong number fo count";printf "%8s%12s%30s\n",NR+1,length($0),w;w=""}'
       LNR         fcount    fcount(new)
       2           1         wrong number fo count
       3           2         wrong number fo count
       4           3         wrong number fo count
       5           4         wrong number fo count
       6           5         wrong number fo count
       7           6         wrong number fo count
       8           7
       9           8         wrong number fo count

This User Gave Thanks to ygemici For This Post:
# 13  
Old 03-30-2012
[QUOTE=ygemici;302613529]
Quote:
Originally Posted by rac

Code:
# sed '1d;$d' file|awk 'BEGIN{printf "%10s%15s%15s\n","LNR","fcount","fcount(new)"}
{if(length($0)!=7)w="wrong number fo count";printf "%8s%12s%30s\n",NR+1,length($0),w;w=""}'
       LNR         fcount    fcount(new)
      2           1         wrong number fo count
      3           2         wrong number fo count
      4           3         wrong number fo count
      5           4         wrong number fo count
      6           5         wrong number fo count
      7           6         wrong number fo count
      8           7
      9           8         wrong number fo count



Now how do i add if else condition in awk?
Say when the character count is 7, we say, "correct count" rather than displaying nothing in the message section. I have tries but something is getting wrong and it says, unable to parse string.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Other user should not read the file but can execute

Hi, I have one script for which I want that other user should not read the script file but can execute. Is there any method ? I tried by giving 711 but it gives Permission denied to other users. For Generic User id as a work around , I have created alias in .bashrc file and other user... (4 Replies)
Discussion started by: Amit Joshi
4 Replies

2. Shell Programming and Scripting

Read user input, Encrypt the data and write to file

Hi, can some one help me how to encrypt and decrypt a file. AIM: reade user input, encrypt it and save it to file. while decryption read the encrypted file decrypt it and save the output in some variable. Example: consider we have Credentials.txt file with content username: password... (5 Replies)
Discussion started by: saichand1985
5 Replies

3. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

4. Programming

Converting a user inputted date to epoch time

Hi all , I need to know how to convert a time stamp entered by the user to be converted to GMT/UTC(epoch time) using mktime() and gmtime() for exapample the input will be put in the form ptm.tm_sec = 0; ptm.tm_min = 59; ptm.tm_hour = 11; ptm.tm_mday = 20;... (2 Replies)
Discussion started by: ada
2 Replies

5. Homework & Coursework Questions

Need User Inputted Date

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I'm making my first program with perl and am supposed to allow the user to either use the current date or enter a... (0 Replies)
Discussion started by: Zamereon
0 Replies

6. Shell Programming and Scripting

Shell script to read lines in a text file and filter user data

hi all, I have this file with some user data. example: $cat myfile.txt FName|LName|Gender|Company|Branch|Bday|Salary|Age aaaa|bbbb|male|cccc|dddd|19900814|15000|20| eeee|asdg|male|gggg|ksgu|19911216||| aara|bdbm|male|kkkk|acke|19931018||23| asad|kfjg|male|kkkc|gkgg|19921213|14000|24|... (4 Replies)
Discussion started by: srimal
4 Replies

7. Shell Programming and Scripting

Substituting a user Inputted word into a command

I am creating a menu driven system and i want to show the last login times of different users, instead of using the 'last' command i wanted to know if there is anyway i could make a brief search tool where the user can input which user they are looking for and then the login times for that specific... (2 Replies)
Discussion started by: warlock129
2 Replies

8. Programming

Not able to see the inputted command on the shell prompt..but its executing the comnd

Hi , I m writing a program which involves piping(pipes). In my program, once i execute the child process (dealing with pipe),I m not able to see any inputted command on the screen....but the entered command is getting executed... Actaully inorder to implement piping i hav closed STDIN and... (3 Replies)
Discussion started by: Crab
3 Replies

9. Shell Programming and Scripting

How do I compare a filesize to an inputted number?

I need to create a shell script that takes 2 arguments, the first argument is any number, and the second argument is a filename. The script needs to indicate if the file's size is bigger or smaller than the number provided. I know it will be some kind of if/then, but I can't figure out how to... (1 Reply)
Discussion started by: Modoc
1 Replies

10. Shell Programming and Scripting

How to hide user inputted text for interactive unix shell script?

Hi everybody, Do you know how to hide the text for interactive unix shell script? Just like the case for inputting password during logon. Patrick (1 Reply)
Discussion started by: patrickpang
1 Replies
Login or Register to Ask a Question