achayan
achayan

Reputation: 11

Unix script to print file in upper case and lower case

I have a file that contains 5000 records mainly names like

david
mathew
philip

I am looking for a script to read that file and create an ouptut like

insert into names_db values(david,DAVID);
insert into names_db values(mathew,MATHEW);
insert into names_db values(philip,PHILIP);

Upvotes: 1

Views: 86

Answers (3)

Michael Jaros
Michael Jaros

Reputation: 4681

This very simple solution uses only Bash for case modification during parameter expansion:

#!/bin/bash

stmt="insert into names_db values(%s,%s);\n"

while read name
do
  printf "$stmt" "$name" "${name^^}"
done < test.txt

Search bash(1) for \^\^ for more information. Note that Bash 4.x or higher is required.

Upvotes: 0

anubhava
anubhava

Reputation: 784868

Using awk:

awk '{printf "insert into names_db values(%s,%s);\n", tolower($0), toupper($0)}' file
insert into names_db values(david,DAVID);
insert into names_db values(mathew,MATHEW);
insert into names_db values(philip,PHILIP);

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174696

A single sed will do this job. You don't need to create a script.

sed 's/.\+/insert into names_db values(&,\U&);/g' file

& refers all the matched characters. \U& would turn all the matched characters to uppercase.

OR

$ sed 's/.*/insert into names_db values(&,\U&);/g' file
insert into names_db values(david,DAVID);
insert into names_db values(mathew,MATHEW);
insert into names_db values(philip,PHILIP);

Upvotes: 1

Related Questions