Formatting a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting a text file
# 1  
Old 02-22-2015
Code Formatting a text file

Hi All Smilie,

I have a formatting question and I am unsure on how I should proceed with my bash shell script. I am unsure weather to use perl or simply edit it in bash. I prefer bash but I am only aware of the awk utility to extract parts of a file, not edit output.

Scenario:

I have a file that has within it text that looks like:

Code:
 Group Username,Username,Username,Username

I am trying to get it to be:

Code:
 group (,username,) (,username,) (,username,)

Any ideas on how I should proceed here?
this is also not case sensitive.

Thanks!
B

Last edited by Don Cragun; 02-22-2015 at 10:05 PM.. Reason: Add CODE tags.
# 2  
Old 02-22-2015
Try this, if you're happy with the result then use mv infile.fixed infile to overwrite the original with the updated version.

Code:
awk 'NF==2 {
   printf "%s ", tolower($1)
   cnt=split(tolower($2),users,",");
   for(u=1;u<=cnt;u++) printf " (,%s,)", users[u]
   printf "\n"
}' infile > infile.fixed

Or this using bash script:

Code:
#!/bin/bash

while read group users
do
   printf "$group"
   ( IFS=,
     for user in $users
     do
       printf " (,$user,)"
     done
   )
   printf "\n"
done < infile > infile.fixed


Last edited by Chubler_XL; 02-22-2015 at 10:26 PM..
# 3  
Old 02-23-2015
Hello bstrizzy,

Following may also help you in same.
Code:
awk -F" |," '{for(i=1;i<=NF;i++){X=i>1?X OFS s1 tolower($i) s2:tolower($i)}} END{print X}' s1="(," s2=",)"  Input_file

Output will be as follows.
Code:
group (,username,) (,username,) (,username,) (,username,)

Thanks,
R. Singh
# 4  
Old 02-23-2015
Special Thanks

Wow. Thanks to Chubler_XL Smilie and RavinderSingh13 Smilie. All of the solutions worked perfectly.

In order to understand this further and practice with 'awk' do you guys recommend any websites/videos/books for tutorials and practice?

Bryan SSmilie

---------- Post updated at 07:19 PM ---------- Previous update was at 04:23 PM ----------

Chubler_XL I am testing this code and Can you please add documentation to this code?

Quote:
awk 'NF==2 {
printf "%s ", tolower($1)
cnt=split(tolower($2),users,",");
for(u=1;u<=cnt;u++) printf " (,%s,)", users[u]
printf "\n"
}' infile > infile.fixed
I am trying to make sense of this and I can only make out:

Line 1 : initiating the awk command and setting the Number of Fields to 2, Why is this being done?

Line 2: We are printing everything in the first field to lowercase but I am unsure what the "%s" represents

Line 3: creating the variable count and setting it equal to splitting what is in field 2 and using the "," as a delimiter. What does the ",users," represent?

Line 4: creating a for loop with random variable 'u' starts at 1 it continues until it has hit the entire count of the second field and 'u' is set to increase 1 everytime. each time it goes through this loop it will print "(,%s,)",users[u]?

Line 5: prints a new line

Line 6: using infile and sending output to infile.fixed

Can you help me fill in my blanks?

Thanks!
B

Last edited by Don Cragun; 02-23-2015 at 10:05 PM.. Reason: Remove duplicated update.
# 5  
Old 02-24-2015
Quote:
Originally Posted by bstrizzy
Wow. Thanks to Chubler_XL Smilie and RavinderSingh13 Smilie. All of the solutions worked perfectly.

In order to understand this further and practice with 'awk' do you guys recommend any websites/videos/books for tutorials and practice?
Start with the awk man page. (Use the command: man awk to get the awk man page on your system.)
Quote:
Bryan SSmilie

---------- Post updated at 07:19 PM ---------- Previous update was at 04:23 PM ----------

Chubler_XL I am testing this code and Can you please add documentation to this code?



I am trying to make sense of this and I can only make out:

Line 1 : initiating the awk command and setting the Number of Fields to 2, Why is this being done?
If it was setting the number of fields, it would be using = instead of ==. This is a test that selects input lines that contain two fields. If the test evaluates to TRUE, the commands between { and the matching } will be executed for this input line. So, in this case all of the commands in this script are executed in order for every line that has two input fields and all other lines are ignored.
Quote:

Line 2: We are printing everything in the first field to lowercase but I am unsure what the "%s" represents
Look at the awk or printf man page for a description of format specifiers in the format string argument to the printf function. In this case, %s prints a string specified by the next argument and then prints a space following that.
Quote:
Line 3: creating the variable count and setting it equal to splitting what is in field 2 and using the "," as a delimiter. What does the ",users," represent?
Unquoted commas separate arguments in any function call. Look at the awk man page. The 1st argument to split tells it what string is to be split; the 2nd argument specifies the name of an array that is to contain the fields that are split out of the 1st argument; and the 3rd argument, if there is one, specifies the extended regular expression that is used to identify the field separators.
Quote:
Line 4: creating a for loop with random variable 'u' starts at 1 it continues until it has hit the entire count of the second field and 'u' is set to increase 1 everytime. each time it goes through this loop it will print "(,%s,)",users[u]?
Again, look at the awk or printf man page to see how the printf function format string argument works. Do not ignore the space in that format argument. As you requested, this loop prints each array element split from the 2nd field on the current input line preceded by a space and the characters (, and followed by ,).
Quote:
Line 5: prints a new line

Line 6: using infile and sending output to infile.fixed
I would say "reading from" instead of "using", but you get the idea.
Quote:
Can you help me fill in my blanks?

Thanks!
B
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 02-26-2015
Quote:
Originally Posted by Don Cragun
Start with the awk man page. (Use the command: man awk to get the awk man page on your system.)

If it was setting the number of fields, it would be using = instead of ==. This is a test that selects input lines that contain two fields. If the test evaluates to TRUE, the commands between { and the matching } will be executed for this input line. So, in this case all of the commands in this script are executed in order for every line that has two input fields and all other lines are ignored.

Look at the awk or printf man page for a description of format specifiers in the format string argument to the printf function. In this case, %s prints a string specified by the next argument and then prints a space following that.

Unquoted commas separate arguments in any function call. Look at the awk man page. The 1st argument to split tells it what string is to be split; the 2nd argument specifies the name of an array that is to contain the fields that are split out of the 1st argument; and the 3rd argument, if there is one, specifies the extended regular expression that is used to identify the field separators.

Again, look at the awk or printf man page to see how the printf function format string argument works. Do not ignore the space in that format argument. As you requested, this loop prints each array element split from the 2nd field on the current input line preceded by a space and the characters (, and followed by ,).

I would say "reading from" instead of "using", but you get the idea.
awesome. sounds like man pages will be my best friend. this was very helpful!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting text file

Hi All, how to format text Thanks (19 Replies)
Discussion started by: ROCK_PLSQL
19 Replies

2. Shell Programming and Scripting

[Solved] Formatting the text file

Hi All, I ahve requirement where I want to put the text file in into proper format. I am wondering how can i achieve that:- Host/Alias Name IP Address Resolved sinuiy01.infra.go2uti.com 10.240.8.158 N sinuid20.devtst.go2uti.com 10.240.8.230 N sinuid21.devtst.go2uti.com... (6 Replies)
Discussion started by: sharsour
6 Replies

3. Shell Programming and Scripting

Comparing and Formatting the text file

hi, I need a script which can format the below text file which contains comments file1.txt -------- //START //Name: some value //Date: //Changes:............. //..................... //END //START //Date: //Name: some value //Changes:............. //..................... (3 Replies)
Discussion started by: flamingo_l
3 Replies

4. Shell Programming and Scripting

Formatting the text file using shell script

How to add the filename to end of each line with | as seperator, except first and last line of the file(s) in directories(with diff tree structure) using shell script?. And also how to replace a list of strings with another set of strings, which is present in a file?. Kindly help out on... (1 Reply)
Discussion started by: av_vinay
1 Replies

5. Shell Programming and Scripting

Formatting text file in unix

Hi, I am using the following format command for formatting my text file in unix. awk -F":" '{ printf "%-50s%-1s%-50s\n", $1,":", $2}' filename > targetfile The target file is then sent as an attachment via email. When I view the target file in notepad multiple lines get spanned as a... (2 Replies)
Discussion started by: AAA
2 Replies

6. Shell Programming and Scripting

Formatting a text file to get data in exact line by line

I have my data something like this SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430693 07/01/2009 05:16:40 FR SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430746 07/01/2009 05:18:05 FR I want the output as follows.... (1 Reply)
Discussion started by: rdhanek
1 Replies

7. UNIX for Dummies Questions & Answers

formatting of the text file

Hi Guys, I have a file with contents in the below format DO_VJ_IDOC;03.23.2009;22:31:09; ZJDO_VJ_IDOC;03.23.2009;22:46:14; ZJDO_RESEND_FAILURES;03.24.2009;01:46:18; Now i need to replace the semicolons with tabs for which i am usig the sed command which gives the O/p as below ... (1 Reply)
Discussion started by: rohit.shetty84
1 Replies

8. UNIX for Dummies Questions & Answers

Text file formatting

Hi all! I'm new in unix, and faced with some difficulties. So I have text file f.e. "textfile" which contains rows like: aaa bbb ccc ddd How could I format it, so the file looks like: aaabbb cccddd Thanks in andvance (5 Replies)
Discussion started by: consta.v
5 Replies

9. Shell Programming and Scripting

Formatting a text file based on newline and delimiter characters

Hi Everybody, I need some help on formatting the files coming into unix box on the fly. I get a file some thing like this in a single line. ISA^M00^M ^M00^M ^M14^M006929681900 ^M01^M095449419 ... (5 Replies)
Discussion started by: ntekupal
5 Replies

10. Shell Programming and Scripting

text file formatting by perl

have a simple text file as input.i have to print that file in paragraph format.whenevr it finds "\n" in the input text it should start printing in next paragraph in output file.also a fixed amount of space should be given before start writing in every paragraph. the input and output file format... (5 Replies)
Discussion started by: avik1983
5 Replies
Login or Register to Ask a Question